Tip for developers: How to handle all of exceptions in your application?

How to handle exceptions that not handled are thrown by application?

 

WPF Application

Navigate to App.xaml.cs file and handle DispatcherUnhandledException event of App class like below

Code Snippet
  1. /// <summary>
  2. /// InitializeComponent
  3. /// </summary>
  4. [System.Diagnostics.DebuggerNonUserCodeAttribute()]
  5. public void InitializeComponent()
  6. {
  7.     this.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);            
  8.     this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
  9. }
  10.  
  11. void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  12. {
  13.     StringBuilder strBuilder = new StringBuilder().AppendLine ("An exception was thrown in your application.");
  14.     strBuilder.AppendLine("Type of exception: " + e.Exception.GetType());
  15.     strBuilder.AppendLine("Exception message: " + e.Exception.Message);
  16.  
  17.     MessageBox.Show(strBuilder.ToString(), "Error",MessageBoxButton.OK, MessageBoxImage.Error);
  18.  
  19.     e.Handled = true;
  20. }

By handling DispatcherUnhandledException event, you can handle all of exceptions is thrown by application that not handled before.

 

Windows Form Application

Navigate to Program.cs file and handle ThreadException event of Application class like below

Code Snippet
  1. static void Main()
  2. {
  3.     Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);            
  4.     Application.EnableVisualStyles();
  5.     Application.SetCompatibleTextRenderingDefault(false);
  6.     Application.Run(new Form1());            
  7. }
  8.  
  9. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  10. {
  11.     MessageBox.Show(e.Exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  12. }

 

 

Limitation

The limitation is if exceptions are thrown from different threat from current thread, they won’t be catch.

If you want to handle exceptions from another thread, you have to handle UnhandledException event from AppDomain class like this

Code Snippet
  1. static void Main()
  2. {
  3.     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  4.  
  5.     Application.EnableVisualStyles();
  6.     Application.SetCompatibleTextRenderingDefault(false);
  7.     Application.Run(new Form1());
  8. }
  9.  
  10. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  11. {
  12.     MessageBox.Show(((Exception)e.ExceptionObject).Message,
  13.                     "Error",
  14.                     MessageBoxButtons.OK,
  15.                     MessageBoxIcon.Error);
  16. }
Published 09-12-2010 4:07 AM by Duy Nguyen
Filed under:
Powered by Community Server (Non-Commercial Edition), by Telligent Systems