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)
{
}
Net classes irrespective of the language they are implemented, then your code should conform to the CLS [Common Language Specification]. This means that your class should only expose features that are common across all . Net languages.
RuntimeWrappedException
, which derives from Exception
.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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With