andrew4582

Unhandled Exception WPF

Jul 1st, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1.   /// <summary>
  2.     /// Interaction logic for App.xaml
  3.     /// </summary>
  4.     public partial class App:Application {
  5.  
  6.         void Application_Startup(object sender,StartupEventArgs e) {
  7.             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  8.         }
  9.  
  10.         private void Application_DispatcherUnhandledException(object sender,System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
  11.  
  12.             try {
  13.                 MessageBox.Show(e.Exception.Message,
  14.                     "Un expected error",
  15.                     MessageBoxButton.OK,
  16.                     MessageBoxImage.Error);
  17.  
  18.             }
  19.             finally {
  20.                 e.Handled = true;
  21.             }
  22.         }
  23.  
  24.  
  25.         void CurrentDomain_UnhandledException(object sender,UnhandledExceptionEventArgs e) {
  26.            
  27.             if(e.IsTerminating)
  28.                 return;
  29.  
  30.             string msg = "Unknown error";
  31.            
  32.             Exception ex = e.ExceptionObject as Exception;
  33.            
  34.             if(ex != null)
  35.                 msg = ex.Message;
  36.            
  37.             MessageBox.Show(msg,"Un expected error",
  38.                 MessageBoxButton.OK,MessageBoxImage.Error);
  39.         }
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment