When developing a windows/console application using .Net, it is recommended to catch AppDomain.CurrentDomain.UnhandledException and Application.ThreadException. If there are some uncaught exceptions raised in a app-domain, the system default handler will report the exception and terminates the application by default, and if raised in a thread, the thread may be blocked. Catching UnhandledException and ThreadException events provides us a way of creating robust applications.
The following code segements demonstrate how to make use of the two events:
1 static void Main() 2 { 3 try { 4 // Setup unhandled exception handlers 5 // 6 AppDomain.CurrentDomain.UnhandledException += 7 new UnhandledExceptionEventHandler(OnUnhandledException); 8 9 // Unhandled Forms exceptions will be delivered to our ThreadException handler 10 // 11 Application.ThreadException += 12 new System.Threading.ThreadExceptionEventHandler(AppThreadException); 13 . 14 } 15 catch ( Exception e ) { } 16 } 17 18 /// <summary> 19 /// CLR unhandled exception 20 /// </summary> 21 private static void OnUnhandledException(Object sender, UnhandledExceptionEventArgs e) 22 { 23 HandleUnhandledException(e.ExceptionObject); 24 } 25 26 /// <summary> 27 /// Displays dialog with information about exceptions that occur in the application. 28 /// </summary> 29 private static void AppThreadException( object source, System.Threading.ThreadExceptionEventArgs e) 30 { 31 HandleUnhandledException(e.Exception); 32 } 33 34 static void HandleUnhandledException(Object o) 35 { 36 Exception exp = o as Exception; 37 MessageBox.Show(exp.Message, " Application Error " , MessageBoxButtons.OK, MessageBoxIcon.Stop); 38 39 Application.Exit(); // Shutting down 40 } posted on 2005-04-23 09:01 Lin 阅读( ...) 评论( ...) 编辑 收藏转载于:https://www.cnblogs.com/losingmyself/archive/2005/04/23/143715.html
相关资源:JAVA上百实例源码以及开源项目