[英]Why doesn't Visual Studio break on unhandled external exceptions?
我正在使用 Visual Studio 2017 (15.4.1) 开发在 .NET Framework 4.6.2 上运行的 ASP.NET Core MVC 2 应用程序。
我试图让 Visual Studio 中断通过我的代码的所有未处理的异常。 如何让 Visual Studio 中断从我的代码调用的外部库中引发的未处理异常?
这些是我的设置:
请参阅以下代码片段:
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 有一个默认的异常处理程序,它将处理我的所有异常。
编辑:既然我的问题已被标记为重复的,让我specifiy为什么它不一样的其他问题。 另一个问题更笼统。 “为什么Visual Studio中并不对未处理的异常中断。 这对我来说非常好。 Visual Studio 确实会因未处理的异常而中断。 Visual Studio仅不会中断从外部库引发的未处理异常,就像我指定的代码示例一样。
其次,原始问题有很多答案,但没有回答原始问题。 (最初的提问者甚至在回复中提到了这一点。)这些其他答案为许多用户解决了一个不同的问题,因此获得了许多赞成票。
问题在于 ASP.NET Core 捕获所有异常,以便能够向客户端显示错误消息,而不是使 HTTP 连接崩溃。
因此,从技术上讲,所有异常都被“处理”,因此不会被 Visual Studio 捕获。
理想情况下,Visual Studio 未处理的异常代码会在 ASP.NET Core 异常处理程序之前插入自己,但到目前为止还没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.