New HandleProcessCorruptedStateExceptions attribute in .NET 4

I am trying to destroy the application . (I also managed to collapse it using the Hans code from How to simulate a corrupt state exception in .NET 4? ".)

The application calls the above emergency code when a button is pressed. Here are my exception handlers:

protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; DispatcherUnhandledException += app_DispatcherUnhandledException; } [HandleProcessCorruptedStateExceptions] void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { //log.. } [HandleProcessCorruptedStateExceptions] void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e) { //log.. } [HandleProcessCorruptedStateExceptions] void app_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { //log.. } 

Comment //log... shown above is just for illustration; there is a real registration code.

When working in Visual Studio, an exception is thrown, but it does not “bubble” up to these exception handler blocks. When working offline (without an attached debugger) I do not get any log, despite what I expect.

Why is this so, and how to make the processing code executed?

+4
source share
3 answers

The attribute must be placed in a method containing try / catch not for event handlers.

In my answer this question is an example

+4
source
  • The goal of FailFast () is to exit immediately, so handlers are not called.
  • Even some of the “corrupt state exceptions” cannot be caught by these handlers - an important example is a StackOverflowException (I actually tried to catch it in an ASP.NET application, but this did not work, although the attribute was present).

The answer is based on this post: www.naveenbhat.in/2013/02/tips-and-tricks-of-exception-handling_28.html

+2
source

The event handler must be marked with both [HandleProcessCorruptedStateExceptions] and [SecurityCritical] for the event handler. This requirement is mentioned in the notes section of FirstChanceException and UnhandledException .

DispatcherUnhandledException notes do not indicate that you can handle damaged state exceptions in it, so this may not be possible with this event.

Also note the notes, it is highly recommended that your FirstChanceException be FirstChanceException inside the constraint area to prevent an endless loop of stack overflows or memory exceptions.

+1
source

Source: https://habr.com/ru/post/1311586/


All Articles