【发布时间】:2018-04-07 15:47:43
【问题描述】:
我正在使用 Visual Studio 2017 (15.4.1) 开发在 .NET Framework 4.6.2 上运行的 ASP.NET Core MVC 2 应用程序。
我正在尝试让 Visual Studio 中断通过我的代码传递的所有未处理异常。如何让 Visual Studio 中断从我的代码调用的外部库中引发的未处理异常?
这些是我的设置:
- 异常设置 -> 抛出时中断:
- 所有默认值。这意味着:
- 几乎所有异常都不会中断。
- 几乎所有异常都在用户代码中未处理时不要继续。
- 所有默认值。这意味着:
- 选项 -> 调试 -> 常规:
- 仅启用我的代码
- 使用新的异常帮助程序。
见以下代码sn-p:
void ThrowExceptionInUserCode()
{
// Exception helper doesn't pop up, and shouldn't.
try { throw new Exception(); }
catch (Exception) { }
// Exception helper pops up, and should.
throw new Exception();
}
void ThrowExceptionInExternalLibrary()
{
// Exception helper doesn't pop up, and shouldn't.
try { PopularLibrary.MethodThatFails(); }
catch (Exception) { }
// Exception helper doesn't pop up, and should.
// This is the thing I'm trying to get to work.
PopularLibrary.MethodThatFails();
}
请注意,我不能禁用“仅我的代码”,因为 ASP.NET 有一个默认的异常处理程序,它将处理我的所有异常。
编辑: 由于我的问题已被标记为重复,让我具体说明为什么它的 不 与其他问题相同。 另一个问题更笼统。 '为什么 Visual Studio 不会在未处理的异常上完全中断。这对我来说非常好。 Visual Studio 确实会在未处理的异常上中断。 Visual Studio only 不会中断从外部库抛出的未处理异常,例如我指定的代码示例。
其次,原题有很多答案,但没有回答原题。 (最初的提问者甚至在回复中提到了这一点。) 这些其他答案为许多用户解决了一个不同的问题,因此得到了很多支持。
【问题讨论】:
-
检查您的
Exception Settings(CTRL+ALT+E)。框Common Language Runtime Exceptions是否带有复选标记(所以所有孩子都被选中)? -
@Igor 不,但如果我检查它,Visual Studio 甚至会在处理异常时中断。我只希望它在未处理异常时中断。
-
好的,我明白了。你看过这个了吗? Visual Studio 2015 break on unhandled exceptions not working
-
@Igor 不,当我尝试一个 Visual Studio 也会在处理的异常上中断。
-
听起来像“只是我的代码”设置
标签: c# exception-handling asp.net-core-mvc visual-studio-2017