Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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)]