Nic*_*ler 5 c# exception asp.net-core-mvc visual-studio-2017
我在开发在 .NET Framework 4.6.2 上运行的 ASP.NET Core MVC 2 应用程序时使用 Visual Studio 2017 (15.4.1)。
我试图让 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();
}
Run Code Online (Sandbox Code Playgroud)
请注意,我无法禁用“仅我的代码”,因为 ASP.NET 有一个默认的异常处理程序,这将使我的所有异常得到处理。
编辑: 由于我的问题已被标记为重复,让我具体说明为什么它与其他问题不同。另一个问题更为普遍。为什么 Visual Studio在未处理的异常上根本不会中断。这对我来说非常有效。Visual Studio 确实会因未处理的异常而中断。Visual Studio仅不会在从外部库引发的未处理异常上中断,就像我指定的代码示例一样。
其次,原问题有很多答案,但没有回答原问题。(最初的提问者甚至在回复中提到了这一点。)这些其他答案为许多用户解决了不同的问题,因此获得了很多赞。
问题在于 ASP.NET Core 捕获所有异常,以便能够向客户端显示错误消息,而不是使 HTTP 连接崩溃。
因此从技术上讲,所有异常都会被“处理”,因此不会被 Visual Studio 捕获。
理想情况下,Visual Studio 未处理的异常代码会将自身插入到 ASP.NET Core 异常处理程序之前,但到目前为止还没有。
归档时间: |
|
查看次数: |
1272 次 |
最近记录: |