【问题标题】:Why doesn't Visual Studio break on unhandled external exceptions?为什么 Visual Studio 不会因未处理的外部异常而中断?
【发布时间】: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


【解决方案1】:

问题在于 ASP.NET Core 捕获所有异常以便能够向客户端显示错误消息,而不是使 HTTP 连接崩溃。

所以从技术上讲,所有异常都被“处理”了,因此不会被 Visual Studio 捕获。

理想情况下,Visual Studio 未处理的异常代码会将自身插入到 ASP.NET Core 异常处理程序之前,但目前还没有。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 2013-06-02
    • 1970-01-01
    • 2019-10-06
    • 2012-10-16
    • 2011-12-02
    • 1970-01-01
    • 2011-09-11
    • 2017-01-27
    相关资源
    最近更新 更多