Exception-Handling Statements

The following section describes the exception-handling statements in C#.

The throw Statement

The throw statement consists of the throw keyword followed by the expression for which an exception is thrown. The exception is thrown when the expression is evaluated. This exception must be an instance of the System.Exception class or any type derived from the same. The following example declares a throw statement that throws an


throw(new ArrayTypeMismatchException());

The expression following the throw keyword is optional. A throw statement that does not have an expression can only be specified in a catch block. The catch block then rethrows the same exception and an appropriate catch block ...

Get Special Edition Using C# now with the O’Reilly learning platform.

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