Throwing Exceptions

As mentioned before, exceptions should be kept for exceptional circumstances where the executing piece of code cannot make any further meaningfully defined attempts to rectify a problematic situation. Big categories of exception types to keep in mind here include input validation and signaling of the inability (for various reasons) to process the call successfully.

Throwing an exception is simply done by use of the throw statement in C#, specifying an object that derives from System.Exception, specifying a meaningful message:

throw new InvalidOperationException("The object is in an invalid state.");

Note: RuntimeWrappedException

The CLR allows any managed object to be thrown as an exception, again for cross-language interoperability ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.