[英]catching specific exceptions coming from another Task method
我有這兩種方法,並且我確定DoSomethingAsync會返回FormatException。 但是,它總是在最后一個捕獲“異常”中捕獲
為什么CallSomethingAsync從不捕獲FormatException?
public Task DoSomethingAsync()
{
//Do something that throws a FormatException
return Task.FromResult(0);
}
public virtual string CallSomethingAsync()
{
try
{
this.DoSomethingAsync().Wait();
return “Ok”;
}
catch (FormatException)
{
return “FormatException”;
}
catch (Exception)
{
return “GeneralException”;
}
}
因為任務可能返回一個或多個異常,所以它拋出一個AggregateException,其中包含在任務執行期間發生的所有異常。
您需要枚舉InnerExceptions集合以顯式處理不同類型的Exception。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.