以下代码在(.NET 4.8项目的WPFUserControl
的代码隐藏(中按预期工作。
private async Task DeleteAsync()
{
try
{
await ViewModel.DeleteAsync(GetSelectedItems());
}
catch (Exception ex)
{
var doCatch = ex is ValidationException
|| ex is ExpectedInfoException
|| ex is ExpectedDbException;
if (doCatch)
ExeptionHelper.HandleException(ex, AppConstants.ApplicationName);
else
throw;
}
}
以下模拟实验并不总是在.NET 6.0项目中捕获异常,请参阅内联注释。我尝试了一个最小限度的可重复样品,但还没有完成,但希望有人已经有了一个有根据的猜测,原因是什么?
private async void DeleteListAsync()
{
var list = ViewModel.GetSelectedItems(
dataGrid.SelectedItems.OfType<ActorModel>().ToList());
try
{
await ViewModel.DeleteListAsync(list);
}
catch (Exception ex) when (
ex is ValidationException
|| ex is ExpectedInfoException
|| ex is ExpectedDbException)
{
/* this part does run as expected when() one of those three
* custom MyNamespace.Exceptions gets thrown in the previous `try` block.
* But unexpectedly *not when thrown inside the nested step-into code of
* ViewModel.DeleteListAsync() */
ApplicationHelper.HandleException(ex, Intl.LocalizedConstants.AppName);
}
catch
{
/* this part does run as expected when a `new InvalidOperationException("Test")`
* or `NotImplementedException()` gets thrown directly in the `try`block.
* Also when thrown inside the nested step-into code of
* ViewModel.DeleteListAsync() */
throw;
}
}
[解决]:很久以后,在有用评论的帮助下,我在嵌套代码的一个隐蔽而黑暗的角落里发现了一个缺失的等待语句,正如@Charlieface所说,非常感谢大家!
您的代码中显然有一个丢失的await
,这导致异常被封装在AggregateException
中。
await
所做的一件事,以及设置状态机,就是打开包含在运行Task
期间抛出的任何异常的AggregateExceptions
。因此,如果您想捕获并处理特定的异常类型,那么应该一直使用await
。