久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

如何捕獲子線程異常

 球球圓圓豆豆 2016-07-05

一 直接在主線程捕獲子線程異常(此方法不可?。?/strong>

[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. }  

代碼執(zhí)行結(jié)果顯示存在“未經(jīng)處理的異?!?。所以使用此方法并不能捕獲子線程拋出的異常。

 

二 在子線程中捕獲并處理異常

[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. }  

使用此方法可以成功捕獲并處理子線程異常,,程序不會(huì)崩潰,。

 

三 子線程中捕獲異常,在主線程中處理異常

[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. }  


使用此方法同樣可以成功捕獲并處理子線程異常,,程序同樣不會(huì)崩潰。

此方法的好處是當(dāng)主線程開(kāi)啟了多個(gè)子線程時(shí),,可以獲取所有子線程的異常后再一起處理。

 

 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買(mǎi)等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多