Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must a type thrown or caught derive from System.Exception

Tags:

So just out of curiosity I wanted to see what was special about the exception class that allowed it to be used with the keyword Throw while a standard class is not.

All I found is that the Exception class implemented the following

public class Exception : System.Object, System.Runtime.Serialization.ISerializable, System.Runtime.InteropServices._Exception { } 

So I tried implementing those same interfaces and attempting to throw my own custom exception that did not derive from System.Exception to no avail. I was simply advised that

The type caught or thrown must be derived from System.Exception

So is there any specific reason for this? I assume there is as few choices in managed languages seem to be arbitrary.

like image 395
Maxim Gershkovich Avatar asked Sep 27 '12 03:09

Maxim Gershkovich


People also ask

What is the point of throwing an exception?

Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then thrown with the throw keyword. The runtime then searches for the most compatible exception handler.

What does exception of type system exception was thrown mean?

SystemException is thrown by the common language runtime when errors occur that are nonfatal and recoverable by user programs. These errors result from failed runtime check (such as an array out-of-bound error), and can occur during the execution of any method.

What does it mean when an exception is thrown?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

When should I throw exception C#?

You'd throw an exception because you anticipated wrong usage without the support of contracts. For example, and IndexOutOfRange exception anticipated a negative index value. If you really don't want a calling method to ignore your error you need to throw an exception.


1 Answers

I think your premise is mistaken. It is possible that an object is thrown that is not derived from System.Exception. You just can't throw it in C# or examine the object in a catch clause. From section 8.10 of the C# spec (v4.0):

Some programming languages may support exceptions that are not representable as an object derived from System.Exception, although such exceptions could never be generated by C# code. A general catch clause may be used to catch such exceptions. Thus, a general catch clause is semantically different from one that specifies the type System.Exception, in that the former may also catch exceptions from other languages.

An example of a general catch:

try { } catch (Exception) { } // 'specific' catch catch { } // 'general' catch 

In particular, this is important when calling unmanaged code.

Some types always seem to get special treatment in every language. Mostly because they are so fundamental to the system. System.Exception, System.ValueType, System.Delegate are all special types in C# that are tightly bound to language keywords and the CLR, so it is not surprising that you can't just implement classes that take over their roles.

like image 146
Mike Zboray Avatar answered Sep 19 '22 15:09

Mike Zboray