Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CLR is Not Allowed Managed Code to Catch Access Violations and Other Corrupted State Exceptions

0.00/5 (No votes)
30 Jan 2014 1  
CLR is not allowed managed code to catch access violations and other corrupted state exceptions

Introduction

When we use .NET 4.0 and above to make an application, then CLR is not allowed managed code to catch access violations and other corrupted state exceptions.

Using the Code

To allow common language runtime, managed code to catch access violations and other corrupted state exceptions, we need to add configuration or we can also set attribute to method to handle corrupted state exceptions.

Use this configuration in your configuration file to allow CLR to handle COM exceptions like corrupted state exceptions:

<configuration>
   <runtime>
      <legacyCorruptedStateExceptionsPolicy enabled="true" />
   </runtime>
</configuration>

This configuration will set for application level, means your code within application can handle COM exception (unmanaged code) but if you want to set any method to handle corrupted state exceptions, you need to write attribute to top of method. This is a code example how we can use attribute:

[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute]
void ReadExcel()
{
try
{
// write code to read excel file
}
catch(Exception ex)
{
// now it will handle exception
}
} 

When I was creating an application in .NET 4.0, then I had to read Excel file. Then I encountered that problem. I had written try catch block in side that method which contains code to read Excel file but my try catch was not working and application got crashed then I reported this issue in Microsoft Visual Studio bug forum, but I did not get any good response from them. Then I searched on the internet and I found this post.

History

  • 30th January, 2014: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here