Unhandled Exceptions


To catch all exceptions from a thread, you surround the initial thread start method with a try/catch/finally block, just as you would for all code within Main(). However, what happens if a third-party component creates an alternate thread and throws an unhandled exception from that thread? A try/catch block in Main() will not catch an exception in an alternate thread. Even if it did, the code could never appropriately recover from all possible exceptions and continue executing. The general unhandled-exceptions guideline is for the program to shut down and restart in a clean state, instead of behaving erratically or hanging because of an invalid state.

Instead of crashing suddenly or ignoring the exception entirely if it occurs on an alternate thread, it is often desirable to save any working data and/or log the exception for error reporting and future debugging. This requires a mechanism to register for notifications of unhandled exceptions.

Registering for unhandled exceptions on the main application domain occurs via an application domain's UnhandledException event. Listing 15.6 demonstrates that process, and Output 15.5 shows the results.

Listing 15.6. Registering for Unhandled Exceptions

 using System; using System.Threading; public class MultiThreadedExceptionHandling {   static void Main()   {        try        {           // Register a callback to receive notifications                          // of any unhandled exception.                                           AppDomain.CurrentDomain.UnhandledException +=                                  OnUnhandledException;                                                    Thread thread =             new Thread(ThrowException);           thread.Start();           // ...           // Wait for the unhandled exception to fire           Thread.Sleep(10000);           Console.WriteLine("Still running...");     }     finally     {         Console.WriteLine("Exiting...");     }  }  static void OnUnhandledException(                                            object sender, UnhandledExceptionEventArgs eventArgs)                  {                                                                             Console.WriteLine("ERROR:{0}", eventArgs.ExceptionObject);           }                                                                        public static void  ThrowException()  {       throw new ApplicationException("Arbitrary exception");  } }

Output 15.5.

Still running... Exiting...

The UnhandledException callback will fire for all unhandled exceptions on threads within the application domain, including the main thread. This is a notification mechanism, not a mechanism to catch and process exceptions so that the application can continue. After the event, the application should be programmed to exit.




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net