阻止 Visual Studio 打破任务中的异常 [英] Stop Visual Studio from breaking on exception in Tasks

查看:27
本文介绍了阻止 Visual Studio 打破任务中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

Task load = Task.Factory.StartNew(() => {//Some Stuff Which Throws an Exception});

try
{
    load.Wait();
}
catch (AggregateException ex)
{
    MessageBox.Show("Error!");
}

每当 Task 中抛出异常时,我都希望它冒泡并被 try catch 捕获,而不是 Visual Studio 在引发异常时中断.

Whenever an exception is thrown in the Task, I want it to bubble up and get caught in the try catch instead of Visual Studio breaking at the point the exception is caused.

我试过谷歌,有人建议我在我的方法之上添加这个 [DebuggerHidden],但它不起作用.

I tried Google and some suggested I add this [DebuggerHidden] on top of my method, but it doesn't work.

推荐答案

好的,我找到了方法.答案是正确的这里在注释部分

Ok I found out how to do it. The answer is right here in the note section

启用仅我的代码"后,Visual Studio 在某些情况下会在引发异常的行中断并显示错误消息,指出异常未由用户代码处理".这个错误是良性的.您可以按 F5 继续并查看这些示例中演示的异常处理行为.要防止 Visual Studio 在第一个错误时中断,只需取消选中工具"、选项"、调试"、常规"下的启用仅我的代码"复选框.

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 Enable Just My Code checkbox under Tools, Options, Debugging, General.

这篇关于阻止 Visual Studio 打破任务中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆