task中出現(xiàn)拋出異常后,后續(xù)流程不執(zhí)行 static void Main(string[] args) { var t = Task.Factory.StartNew(() => { Console.WriteLine("1"); Console.WriteLine("2"); try { Console.WriteLine("3"); string str = tt(); Console.WriteLine(str); Console.WriteLine("4"); } catch (Exception ex) { Console.WriteLine("報(bào)錯(cuò)顯示:{0}", ex); } Console.WriteLine("5"); Console.WriteLine("6"); }); // tt方法報(bào)錯(cuò)后,進(jìn)入catch中后,在沒(méi)有ContinueWith時(shí),下邊的5、6不會(huì)執(zhí)行. // 如果存在ContinueWith,則有所有流程正常執(zhí)行 t.ContinueWith(x => { Console.WriteLine("關(guān)閉進(jìn)程后執(zhí)行1"); }); Console.WriteLine("關(guān)閉進(jìn)程后執(zhí)行2"); Console.WriteLine("關(guān)閉進(jìn)程后執(zhí)行3"); Console.WriteLine("完成"); Console.ReadLine(); } public static string tt() { throw new IndexOutOfRangeException(); } |
|