Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C# 4.0
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.85/5 (11 votes)
30 Jan 2014CPOL1 min read 25.5K   6   4
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:

XML
<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:

C#
[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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Bharti Airtel
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Vinicius G. D. Menezes31-Jan-14 5:27
professionalVinicius G. D. Menezes31-Jan-14 5:27 
QuestionUseful... but a bit misleading Pin
Nicholas Marty31-Jan-14 0:07
professionalNicholas Marty31-Jan-14 0:07 
GeneralMy vote of 5 Pin
Itay Gal30-Jan-14 23:19
Itay Gal30-Jan-14 23:19 
GeneralMy vote of 5 Pin
Klaus Luedenscheidt30-Jan-14 18:53
Klaus Luedenscheidt30-Jan-14 18:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.