当从Task.Run(...).Result中抛出自定义异常时,catch块会找到AggregateException而不是CustomException。为什么?
public MainPage()
{
this.InitializeComponent();
CallAMethod();
}
static bool AMethod()
{
return Task.Run(() =>
{
throw new CustomException();
return false;
}).Result;
}
static bool CallAMethod()
{
try
{
return AMethod();
}
catch (CustomException e)
{
//not caught
throw;
}
catch (Exception ex)
{
//caught?
throw;
}
}
这里是自定义异常类
class CustomException : Exception
{
}