class Program { public static Thread thread1; static void Main(string[] args) { thread1 = new Thread(Method1); thread1.Start(); Console.ReadKey(); } public static void Method1() { try { for (int i = 0; i < 10; i++) { Console.WriteLine("Mthod1: " + i.ToString()); Thread.Sleep(1000); if (i == 3) { Thread.CurrentThread.Abort(); // 拋出的ThreadAbortException異常 } Console.WriteLine("Mthod1: " + i.ToString() + " End"); } } catch (SocketException ex) { Console.WriteLine("Method1 SocketException: " + ex.ToString()); } catch (ThreadAbortException ex) { // ThreadAbortException要在Exception的前面,由於Exception可以匹配全部異常 Console.WriteLine("Method1 ThreadAbortException: " + ex.ToString()); } catch (Exception ex) { Console.WriteLine("Method1 Exception: " + ex.ToString()); } finally { Console.WriteLine("Method1 Finally:"); } } }
執行結果:spa
一、thread1.Abort()執行後會直接拋出ThreadAbortException異常。線程
二、異常會中止後續全部程序的執行(沒有輸出"Method1: 3 End")。3d
三、catch語句會執行。catch (Exception ex)和catch (ThreadAbortException ex)均可以捕獲異常,因爲Exception能夠匹配全部異常,所以catch (ThreadAbortException ex)應該在catch (Exception ex)的前面使用,因此輸出了「"Method1 ThreadAbortException」。code
四、finally語句會執行。blog
class Program { public static Thread thread1; static void Main(string[] args) { thread1 = new Thread(Method1); thread1.Start(); Console.ReadKey(); } public static void StopMethod1() { try { thread1.Abort(); // 首先捕獲拋出的ThreadAbortException異常 } catch (Exception ex) { Console.WriteLine("StopMethod1: " + ex.ToString()); } } public static void Method1() { try { for (int i = 0; i < 10; i++) { Console.WriteLine("Mthod1: " + i.ToString()); Thread.Sleep(1000); if (i == 5) StopMethod1(); // 再次捕獲拋出的ThreadAbortException異常 } } catch (Exception ex) { Console.WriteLine("Method1: " + ex.ToString()); } } }
運行結果:進程
try catch{} 捕獲兩次拋出的ThreadAbortException。string
class Program { public static Thread thread1; static void Main(string[] args) { thread1 = new Thread(Method1); thread1.Start(); Console.ReadKey(); } public static void StopMethod1() { thread1.Abort(); } public static void Method1() { for (int i = 0; i < 10; i++) { Console.WriteLine("Mthod1: " + i.ToString()); Thread.Sleep(1000); if (i == 5) StopMethod1(); } } }
運行結果:it
一、雖然線程拋出了ThreadAbortException異常,因爲線程中沒有使用try catch的語句捕獲異常,因此看上去線程很平靜的退出了。io
二、並且,因爲是在線程中拋出的異常,因此主進程沒有任何錯誤提示。for循環
三、for循環只輸出到5,Abort()後通常線程會直接中止。
class Program { public static Thread thread1; public static Thread thread2; static void Main(string[] args) { thread1 = new Thread(Method1); thread2 = new Thread(Method2); thread1.Start(); thread2.Start(); Console.ReadKey(); } public static void StopMethod1() { try { thread2.Abort(); } catch (Exception ex) { Console.WriteLine("StopMethod1: " + ex.ToString()); } } public static void Method1() { try { for (int i = 0; i < 10; i++) { Console.WriteLine("Method1: " + i.ToString()); Thread.Sleep(1000); if (i == 5) StopMethod1(); } } catch (Exception ex) { Console.WriteLine("Method3: " + ex.ToString()); } } public static void Method2() { try { for (int i = 0; i < 10; i++) { Console.WriteLine("Method2: " + i.ToString()); Thread.Sleep(1000); } } catch (Exception ex) { Console.WriteLine("Method2: " + ex.ToString()); } } }
運行結果:
一、能夠看到只在thread2中捕獲到了ThreadAbortException異常。
二、thread1完整的運行完畢了,而且沒有ThreadAbortException異常拋出。
一、theadX.Abort()方法會在線程X中拋出ThreadAbortException異常,線程X中全部正在執行的try catch都會捕獲該異常。
二、theadX.Abort()有可能在Y線程中調用的,不會在Y線程中拋出ThreadAbortException異常。