我有一些代碼,捕捉由服務器調用拋出如下所有異常:不可觀測異常被再次拋出,儘管等待的任務和捕捉異常
new public Task SaveAsync()
{
return ServerException.Wrap(base.SaveAsync);
}
凡ServerException.Wrap樣子:
public static async Task<T> Wrap<T>(Func<Task<T>> func)
{
try
{
return await func();
}
catch (Exception ex)
{
// This is an internal error that shouldn't happen.
throw new ServerException(ex);
}
}
public static async Task Wrap(Func<Task> func)
{
await Wrap(async() =>
{
await func();
return true;
});
}
然後,我有一個調用SaveAsync如下功能:
try
{
await problem.SaveAsync();
}
catch (ServerException ex)
{
Logger.LogException("Error saving problem.", ex);
}
我有一些內部錯誤,產生一個異常,我在上面的線,然後它記錄如下:
2015-10-20 11:20:44.502 [第99行]錯誤保存問題。 (例外: Exceptions.ServerException:---> System.ArgumentException:用相同鍵的項 已添加在 System.ThrowHelper.ThrowArgumentException(ExceptionResource資源) [0x00000]在 /用戶/助洗劑/數據/車道/ 1977年/ 2c66d2fe /源/單聲道/外部/ referencesource/mscorlib程序/系統/ throwhelper.cs:74
然而幾秒鐘後,我得到的是被記錄的unhanded異常警告:
2015-10-20 11:21:16.352警告:未處理的異常: System.AggregateException:通過等待任務或訪問其Exception屬性,未觀察到任務的異常 。由於 結果,未被觀察到的異常被終結者 線程重新拋出。 ---> System.ArgumentException:具有相同密鑰的項目已添加 。在System.ThrowHelper.ThrowArgumentException (ExceptionResource資源)[0x00000]在 /Users/builder/data/lanes/1977/2c66d2fe/source/mono/external/referencesource/mscorlib/system/throwhelper.cs:74
爲什麼我會得到第二個未觀察到的異常,即使我正在捕捉並處理第一個異常?這個異常似乎是由我的ServerException.Wrap方法引發的。
我正在使用MonoTouch。
你確定這是所有相關的代碼? – i3arnon
這是簡化版本。你需要哪些其他代碼部分? –
任務未等待的部分:) SaveAsync如何顯示? – i3arnon