Corrupted State Exceptions Handling in .NET 4.0

Corrupted state exceptions are Structured Exception Handling (SEH) Exceptions  that indicate the state of a process has been corrupted. CSEs can be considered as Super Exceptions are caused due to bug in the CLR itself or in some native code for which the managed developer has no control over. These are considered way different from other Exceptions. The CLR4 considers exceptions thrown by native code as corrupted state exceptions. By default, the CLR will not let managed code catch these exceptions and finally blocks will not execute.

System.Runtime.ExceptionServices namespace in .NET4 that adds the ability to handle corrupted state exceptions.

Handling CSEs - Implementation

There might be some rare cases in which we need to handle Corrupted State Exceptions. We can do it using a new attribute called HandleProcessCorruptedStateExceptions from System.Runtime.ExceptionServices namespace. This is used to log that something went wrong or send a notification via email about these exceptions, without continuing processing.

Step1 :

Import the namespace System.Runtime.ExceptionServices.

Step2:

Individual managed methods can override the defaults and catch these exceptions by applying the HandleProcessCorruptedStateExceptions attribute to the method.

[HandleProcessCorruptedStateExceptions]

public static void HandleCorruptedState()

{    

// Write Exception Notification code here  

//  can log or send mail to admin etc.

}