Will CLR handle both CLS-compliant and non-CLS compliant exceptions?

Just for my clarification:

Can I throw both CLS-compliant and non-CLS compliant exceptions in .NET Framework? I am using C# 3.0.

When I catch an exception

catch(Exception ex)
{

}
  • Will it catch only CLS-compliant exceptions?
  • What is the use of RuntimeWrappedException class (can I have a simple example?).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

  1. Yes, you can throw non-CLS compliant exceptions in the .NET Framework, as long as you use a language that allows it, e.g. C++.
  2. No, you cannot throw non-CLS compliant exceptions in C#.
  3. No, it will catch all exceptions, since any non-CLS compliant exception will be wrapped in a RuntimeWrappedException, which derives from Exception.
  4. You do not throw a RuntimeWrappedException, the CLR does. MSDN has a detailed description, which I quote here:

    Some languages, such as C++, allow you to throw exceptions of any managed type. Other languages, such as Microsoft C# and Visual Basic, require that every thrown exception be derived from the Exception class. To maintain compatibility between languages, the common language runtime (CLR) wraps objects that do not derive from Exception in a RuntimeWrappedException object.

If you want to treat CLS and non-CLS exceptions differently, just catch them in the right order:

try {
    // some code
catch(RuntimeWrappedException ex) {
    // non-CLS exceptions
catch(Exception ex) {
    // CLS exceptions
}

Method 2

Although the C# compiler allows developers to throw Exception-derived objects only, prior to
C# version 2.0, the C# compiler did allow developers to catch non-CLS-compliant exceptions
by using code like this:

private void SomeMethod() { 
try { 
// Inside the try block is where you put code requiring 
// graceful recovery or common cleanup operations. 
}
catch (Exception e) { 
// Before C# 2.0, this block catches CLS-compliant exceptions only 
// In C# 2.0, this block catches CLS- & non-CLS- compliant exceptions 
throw; // Re-throws whatever got caught 
}
catch { 
// In all versions of C#, this block catches 
// CLS- & non-CLS- compliant exceptions 
throw; // Re-throws whatever got caught 
}
}

Method 3

The CLS allows it but not all languages do. It can be done with C++/CLI but it can’t with C#, VB.NET.

Starting with .Net Framework 2.0, all non-CLS exceptions are wrapped inside a RuntimeWrappedException.

Since RuntimeWrappedException derives from Exception, a catch (Exception) will catch non-CLS exceptions.

From a RuntimeWrappedException you can access the original exception through RuntimeWrappedException.WrappedException property.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x