creating a base exception for a windows application

  • Thread starter Thread starter CharlieC
  • Start date Start date
C

CharlieC

I am writing a windows C# application, and I want to ensure that all
exceptions, handled or otherwise, are logged to the local event log.
I can do this for the handled exceptions, but I am not sure how to
override (?) the system exception such that un-handled exceptions are
logged.
I know that you can do this in delphi by pointing the application
exception to your own base exception handler. Is there any similar way
that this can be done in the dot.net V2 framework?

Many thanks in advance
 
I usually create a class, called


EntryPoint.cs


[STAThread]
static void Main(string[] args)
{
try
{


Application.Run(new Form1()); //Whatever your startup form is


}


}
catch (Exception ex)
{
//Notify User
MessageBox.Show( "A (uncaught) exception has occured. MyApp
cannot continue." );
//Shut down application
Application.Exit( );
}
}


This will catch any uncaught exceptions.




You should should google
try catch finally brad abrams


and you can read where


"You should be writing many many more


try/finally
blocks


and not so many
try/catch/finally


blocks.






You should also check out
http://www.microsoft.com/downloads/...FamilyID=8CA8EB6E-6F4A-43DF-ADEB-8F22CA173E02
or
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/ehab.asp

Its default behavior is to write to the EventLog, but you can EXPAND this if
you need to.

I wrote my own custom publisher which allows any/all of 3 things:

Write a .log file on users local machine.
Send an email.
Update a db with insertion.


While the EMAB (Exception Management Application Block) is older, its still
good stuff.



The EMAB basically requires some basic app.config setup. and then you add
ONE line of code.

altered from above

catch (Exception ex)
{

ExceptionManager.Publish(ex); // Yes, this is it. Even for my
..logfile, emailer, db update, this is all I put in my code. I set up
everything in app.config.

//Notify User
MessageBox.Show( "A (uncaught) exception has occured. MyApp
cannot continue." );
//Shut down application
Application.Exit( );
}
 
CharlieC said:
I am writing a windows C# application, and I want to ensure that all
exceptions, handled or otherwise, are logged to the local event log.
I can do this for the handled exceptions, but I am not sure how to
override (?) the system exception such that un-handled exceptions are
logged.
I know that you can do this in delphi by pointing the application
exception to your own base exception handler. Is there any similar way
that this can be done in the dot.net V2 framework?

Many thanks in advance

You want to handle two events: AppDomain.UnhandledException, and
Application.ThreadException. See the following article:

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

and the relevant MSDN help for these two events.

However, you don't want to log handled (caught) exceptions. There are
several reasons to catch an exception, but in every case it means that
the application has successfully dealt with the problem and is moving
on. Logging caught exceptions is like having your app write log lines
saying, "I'm still working OK..." "I'm still working OK..." ad nauseum.

If you're catching exceptions that really mean that the application is
sick and is going to shut down, then you really shouldn't be (unless
you have to do something special before the application shuts down...
and even then the above two event handlers are likely better places to
do that).
 
Charlie,
Here is a simplified example that may help:

[STAThread]
static void Main()
{

Application.ThreadException += new
ThreadExceptionEventHandler(OnThreadException);
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.EnableVisualStyles();
Application.Run(new Form1());
}

// Handles the exception event for all other threads
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{

LogException(e.ExceptionObject);
}

// Handles the exception event from a UI thread.
static void OnThreadException(object sender,
ThreadExceptionEventArgs t)
{

LogException(t.Exception);
}


private LogException (Exception e)
{
// your cool logging code here
}

--Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top