Visual Studio 2015 unexpectedly breaking on handled exceptions

允我心安 提交于 2019-12-07 05:37:17

问题


An image being worth a lot of words, how is the following possible:

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).

The line failing and causing the break is a call to an external API (which is somewhat buggy, hence the try/catch block).

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.


回答1:


There is a special case for this exception, which I am guessing applies here. From the docs:

AccessViolationException and try/catch blocks

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. To handle such an AccessViolationException exception, you should apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method in which the exception is thrown. This change does not affect AccessViolationException exceptions thrown by user code, which can continue to be caught by a catch statement. 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. Note that you can also receive notification of the exceptions if you have defined a handler for the AppDomain.FirstChanceException or AppDomain.UnhandledException event.

As the docs say, the solution is to add the HandleProcessCorruptedStateExceptionsAttribute to the Start() method. If not possible (e.g., 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.




回答2:


You can see the answer in the following link How to handle 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.

Enjoy



来源:https://stackoverflow.com/questions/39575449/visual-studio-2015-unexpectedly-breaking-on-handled-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!