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
{
}
catch(Exception ex)
{
}
}
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