MrMistreater

Catching unhandled exceptions

May 10th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. static void Main()
  2. {
  3.     Application.EnableVisualStyles();
  4.     Application.SetCompatibleTextRenderingDefault(false);
  5.     Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
  6.     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  7.     Application.Run(new Form1());
  8. }
  9.  
  10. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  11. {
  12.     MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
  13.     // here you can log the exception ...
  14. }
  15.  
  16. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  17. {
  18.     MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
  19.     // here you can log the exception ...
  20. }
Advertisement
Add Comment
Please, Sign In to add comment