Task 編程中的異常處理

來源:互聯網
上載者:User

在 .Net 開發中, 使用 Task 、 Task<T> 進行非同步編程是非常方便的, 但是在處理 Task 產生的異常時, 需要注意一個問題, 比如下面的代碼:

static Task<int> TestAsync(int a, int b) {   var tcs = new TaskCompletionSource<int>();   Task.Factory.StartNew(() => {      if (a + b < 0) {         tcs.TrySetException(new InvalidOperationException("a + b < 0"));      }      else {         tcs.TrySetResult(a + b);      }   });   return tcs.Task;}

當輸入的兩個參數之和小於 0 時, tcs 會設定一個 InvalidOperationException , 如果直接運行這段代碼, 當這個函數返回的 Task 被 GC 回收時, 將會產生 AggregateException was unhandled 的異常, 運行代碼如下:

static void Main(string[] args) {   TestAsync(5, -10);   Thread.Sleep(TimeSpan.FromMilliseconds(3000));   GC.Collect();   Console.WriteLine("Completed.");}

當程式運行結束時, 會產生所示的異常:

關鍵的是這段文字:

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.

沒有在等待 Task 完成時捕獲其異常, 也沒有讀取 Task 的 Exception 屬性, 結果導致異常被終結線程重新拋出。 也就是說, Task 異常有兩種處理方式:

1、 調用 Task 的 Wait 方法時使用 try-catch 捕獲異常:

var testTask = TestAsync(5, -10);try {   testTask.Wait();}catch(Exception ex) {   Console.WriteLine(ex);}

2、 在 Task 的 ContinueWith 方法中讀取 Task 的 Exception 屬性:

var testTask = TestAsync(5, -10);testTask.ContinueWith(task => {   if (task.IsFaulted) {      Console.WriteLine(task.Exception.GetBaseException());   }   else {      Console.WriteLine(task.Result);   }});

在 .Net 4.0 、 Sliverlight 5.0 、以及 MonoTouch 中均有類似的問題, 因此, 必須小心翼翼的處理 Task 產生的異常, 否則將會導致你的程式異常退出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.