gpt4 book ai didi

C# - 未捕获任务异常

转载 作者:行者123 更新时间:2023-11-30 18:59:12 25 4
gpt4 key购买 nike

我确定我做错了什么,但是当我尝试这个新的线程任务异常处理示例时,我总是收到用户代码未处理的异常。代码的重点是展示如何捕获任务中的错误的示例。

链接:Task Exception Example

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);

最佳答案

来自您引用的页面:

Note

When "Just My Code" is enabled, Visual Studio in some cases will break on the line that throws the exception and display an error message that says "exception not handled by user code." This error is benign. You can press F5 to continue and see the exception-handling behavior that is demonstrated in these examples. To prevent Visual Studio from breaking on the first error, just uncheck the "Just My Code" checkbox under Tools, Options, Debugging, General.

关于C# - 未捕获任务异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com