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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.