分享

如何捕获子线程异常

 球球圆圆豆豆 2016-07-05

一 直接在主线程捕获子线程异常(此方法不可取)

[csharp] view plain copy
  1. using System;  
  2. using System.Threading;  
  3. namespace CatchThreadException  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             try  
  10.             {  
  11.                 Thread t = new Thread(() =>  
  12.                 {  
  13.                     throw new Exception("我是子线程中抛出的异常!");  
  14.                 });  
  15.                 t.Start();  
  16.             }  
  17.             catch (Exception ex)  
  18.             {  
  19.                 Console.WriteLine(ex.Message);  
  20.             }  
  21.         }  
  22.     }  
  23. }  

代码执行结果显示存在“未经处理的异常”。所以使用此方法并不能捕获子线程抛出的异常。

 

二 在子线程中捕获并处理异常

[csharp] view plain copy
  1. using System;  
  2. using System.Threading;  
  3. namespace CatchThreadException  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Thread t = new Thread(() =>  
  10.             {  
  11.                 try  
  12.                 {  
  13.                     throw new Exception("我是子线程中抛出的异常!");  
  14.                 }  
  15.                 catch (Exception ex)  
  16.                 {  
  17.                     Console.WriteLine("子线程:" +   
  18. ex.Message);  
  19.                 }  
  20.             });  
  21.             t.Start();  
  22.         }  
  23.     }  
  24. }  

使用此方法可以成功捕获并处理子线程异常,程序不会崩溃。

 

三 子线程中捕获异常,在主线程中处理异常

[csharp] view plain copy
  1. using System;  
  2. using System.Threading;  
  3. namespace CatchThreadException  
  4. {  
  5.     class Program  
  6.     {  
  7.         private delegate void ThreadExceptionEventHandler(Exception ex);  
  8.         private static ThreadExceptionEventHandler exceptionHappened;  
  9.         private static Exception exceptions;  
  10.         static void Main(string[] args)  
  11.         {  
  12.             exceptionHappened = new ThreadExceptionEventHandler(ShowThreadException);  
  13.             Thread t = new Thread(() =>  
  14.                 {  
  15.                     try  
  16.                     {  
  17.                         throw new Exception("我是子线程中抛出的异常!");  
  18.                     }  
  19.                     catch (Exception ex)  
  20.                     {  
  21.                         OnThreadExceptionHappened(ex);  
  22.                     }  
  23.                 }            
  24.             );  
  25.             t.Start();  
  26.             t.Join();  
  27.             if (exceptions != null)  
  28.             {  
  29.                 Console.WriteLine(exceptions.Message);  
  30.             }  
  31.         }  
  32.         private static void ShowThreadException(Exception ex)  
  33.         {  
  34.             exceptions = ex;  
  35.         }  
  36.         private static void OnThreadExceptionHappened(Exception ex)  
  37.         {  
  38.             if (exceptionHappened != null)  
  39.             {  
  40.                 exceptionHappened(ex);  
  41.             }  
  42.         }  
  43.     }  
  44. }  


使用此方法同样可以成功捕获并处理子线程异常,程序同样不会崩溃。

此方法的好处是当主线程开启了多个子线程时,可以获取所有子线程的异常后再一起处理。

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多