Welcome to MSDN Blogs Sign in | Join | Help
Non-CLS Exceptions

Many developers are not aware that according to CLR it is perfectly OK to throw exceptions which are NOT derived from System.Exception: Exceptions such as DateTime, Int64 and String. Although languages such as C# do not allows programmers to throw these types of exceptions it should still be possible to catch non-CLS compliant exceptions using a catch block.

Pre to CLR version 2.0, the only way to catch non-CLS compliant exceptions was to introduce a catch block with no Exception section:

try

{

   // Some code here which might

   // throw non-CLS compilant exception

}

catch (Exception ex)

{

   // Here we will be catching any

   // CLS compliant type of exceptions

}

catch

{

   // Here we will be catching any

   // non-CLS compliant exceptions...

   // Note that, this block on its own

   // is capable of catching both compliant

   // and non-compinat exceptions

}

In version 2.0 of CLR, any non-CLS compliant exception is automatically wrapped up by System.Runtime.CompilerServices.RuntimeWrappedException class which inherits from the Exception class. This class has a property which is set to the actual object which was thrown.

It is still possible to tell CLR not to wrap up the non-CLS compliant exceptions with the RuntimeWrappedException class. This is done using an attribute which affects the whole assembly:

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = false)]

Posted: Sunday, January 07, 2007 1:56 PM by pedramr
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker