简体   繁体   English

Visual Studio 2015意外地破坏了处理的异常

[英]Visual Studio 2015 unexpectedly breaking on handled exceptions

An image being worth a lot of words, how is the following possible: 一张图片值得一提,以下是可能的:

Visual Studio 2015打破它不应该

As can be seen, Visual Studio 2015 (latest version) breaks while Common Language Runtime Exceptions under Exception Settings is unchecked, Enable Just My Code under Tools > Options > Debugging is checked, and the exception is clearly handled (within a try/catch block). 可以看出,Visual Studio 2015(最新版本)中断了Exception Settings下的Common Language Runtime Exceptions ,选中了Tools > Options > Debugging下的Enable Just My Code ,并且明确处理了异常(在try / catch块中) )。

The line failing and causing the break is a call to an external API (which is somewhat buggy, hence the try/catch block). 该行失败并导致中断是对外部API的调用(这有点错误,因此是try / catch块)。

Am I missing something that would justify the break or is this a bug? 我错过了一些可以证明休息的理由或者这是一个错误吗? I thought this other question would provide some insight but it unfortunately does not help here (the exception is handled so we should not need to enable the additional Continue When Unhandled in User Code option. 我认为这个其他问题会提供一些见解,但遗憾的是它没有帮助(处理异常因此我们不需要Continue When Unhandled in User Code选项中启用额外的“ Continue When Unhandled in User Code

There is a special case for this exception, which I am guessing applies here. 这个例外有一个特例,我猜这里适用。 From the docs : 来自文档

AccessViolationException and try/catch blocks AccessViolationException和try / catch块

Starting with the .NET Framework 4, AccessViolationException exceptions thrown by the common language runtime are not handled by the catch statement in a structured exception handler if the exception occurs outside of the memory reserved by the common language runtime. 从.NET Framework 4开始,如果异常发生在公共语言运行库保留的内存之外,则结构化异常处理程序中的catch语句不会处理公共语言运行库抛出的AccessViolationException异常。 To handle such an AccessViolationException exception, you should apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method in which the exception is thrown. 若要处理此类AccessViolationException异常,应将HandleProcessCorruptedStateExceptionsAttribute属性应用于引发异常的方法。 This change does not affect AccessViolationException exceptions thrown by user code, which can continue to be caught by a catch statement. 此更改不会影响用户代码抛出的AccessViolationException异常,这些异常可以继续被catch语句捕获。 For code written for previous versions of the .NET Framework that you want to recompile and run without modification on the .NET Framework 4, you can add the element to your app's configuration file. 对于为.NET Framework的先前版本编写的代码,您希望在.NET Framework 4上重新编译和运行而不进行修改,您可以将该元素添加到应用程序的配置文件中。 Note that you can also receive notification of the exceptions if you have defined a handler for the AppDomain.FirstChanceException or AppDomain.UnhandledException event. 请注意,如果已为AppDomain.FirstChanceException或AppDomain.UnhandledException事件定义了处理程序,则还可以接收异常通知。

As the docs say, the solution is to add the HandleProcessCorruptedStateExceptionsAttribute to the Start() method. 正如文档所说,解决方案是将HandleProcessCorruptedStateExceptionsAttribute添加到Start()方法。 If not possible (eg, this is supplied via a library), I'm guessing you can add a method that wraps the call and add the attribute to that wrapping method. 如果不可能(例如,这是通过库提供的),我猜你可以添加一个包装调用的方法,并将该属性添加到该包装方法。

You can see the answer in the following link How to handle AccessViolationException 您可以在以下链接中查看答案如何处理AccessViolationException

For many reasons, in .NET 4.0 the runtime handles some exceptions as Windows Structured Error Handling (SEH) errors as indicators for Corrupted State, and those can not be caught as regular exceptions. 由于许多原因,在.NET 4.0中,运行时将一些异常处理为Windows结构化错误处理(SEH)错误,作为损坏状态的指示符,并且这些异常不能作为常规异常捕获。

Enjoy 请享用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM