
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
432
|
Chapter 7: Exception Handling
The event handler code is established in the CurrentDomain_UnhandledException
and OnThreadException handler methods. See Recipe 7.10 for more information on
the
UnhandledExceptionEventHandler. The ThreadExceptionEventHandler is passed the
sender object and a
ThreadExceptionEventArgs object. ThreadExceptionEventArgs has an
Exception property that contains the unhandled exception from the WinForms UI
thread.
// Handles the exception event for all other threads
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
// Just show the exception details.
MessageBox.Show("CurrentDomain_UnhandledException: " +
e.ExceptionObject.ToString( ));
}
// Handles the exception event from a UI thread
static void OnThreadException(object sender, ThreadExceptionEventArgs t)
{
// Just show the exception details.
MessageBox.Show("OnThreadException: " + t.Exception.ToString( ));
}
Discussion
Exceptions are the primary way to convey errors in .NET, so when you build an
application it is imperative that there be a final line of defense against unhandled
exceptions. An unhandled exception will crash the program (even if it looks a bit
nicer in .NET); this is not the impression you wish to make on your customers. It
would have been nice to have