[英]C# - Task Exception not being caught
我确定我做错了什么,但是当我尝试新线程任务异常处理的这个示例时,我总是得到用户代码未处理的异常。 该代码的全部重点是显示一个如何捕获任务错误的示例。
链接: 任务异常示例
static void Main(string[] args)
{
var task1 = Task.Factory.StartNew(() =>
{
throw new MyCustomException("I'm bad, but not too bad!");
});
try
{
task1.Wait();
}
catch (AggregateException ae)
{
// Assume we know what's going on with this particular exception.
// Rethrow anything else. AggregateException.Handle provides
// another way to express this. See later example.
foreach (var e in ae.InnerExceptions)
{
if (e is MyCustomException)
{
Console.WriteLine(e.Message);
}
else
{
throw;
}
}
}
}
最有可能的用户错误只是不确定是什么(使用Visual Studio 2012);
在您引用的页面中:
注意
启用“仅我的代码”后,Visual Studio在某些情况下会在引发异常的行上中断,并显示一条错误消息,指出“用户代码未处理异常”。 此错误是良性的。 您可以按F5键继续并查看这些示例中演示的异常处理行为。 要防止Visual Studio遇到第一个错误,只需取消选中“ 工具”,“选项”,“调试”,“常规 ”下的“仅我的代码”复选框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.