Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 23: Exceptions - Pg. 645

612 23 Exceptions Exceptions in C# provide a structured, uniform, and type-safe way of handling both system level and application-level error conditions. [Note: The exception mechanism in C# is quite similar to that of C++, with a few important differences: In C#, all exceptions shall be represented by an instance of a class type derived from Sys- tem.Exception. In C++, any value of any type can be used to represent an exception. In C#, a finally block (§15.10) can be used to write termination code that executes in both normal execution and exceptional conditions. C++ has no equivalent construct. In C#, system-level exceptions such as overflow, divide-by-zero, and null dereferences have well defined exception classes and are on a par with application-level error conditions. end note] Exceptions not advised as control flow Some might consider using exceptions as another form of flow-control--for example, as a way to provide a multilevel return that cancels a whole stack of method invocations. In general, this is not advised. Throwing an exception is comparatively very slow--maybe 20,000 cpu cycles--on Microsoft's current CLR (and Mono is believed to behave similarly), and much slower than in most Stan- dard ML implementations, for example. This suggests that: 1. A performance-conscious developer should make sure that exceptions are not thrown on frequent execution paths; 2. A performance-conscious library designer should always give clients an exception-free way to test whether something might fail--for example, the C5 library follows this guideline; and 3. The designers of CLI and C# did not intend exceptions to be used for control flow, the former for allowing them to be slow and the latter for adopting the CLI implementation. The reason for the slowness? It is partly that when an exception is thrown on the CLI, a stack trace gets incorporated into the exception object. This suggests that one of the expected uses of exceptions was to assist debugging, in addition to their normal use to handle exceptional situations during normal program execution. Note that, in Java, the stack trace gets incorpo- rated when the exception object is created, not thrown. This allows an optimization in Java when the stack trace is known to be ignored: Create the exception once and for all, and throw it where needed. 23.1 Causes of exceptions Exceptions can be thrown in two different ways. A throw statement (§15.9.5) throws an exception immediately and unconditionally. Con- trol never reaches the statement immediately following the throw. Certain exceptional conditions that arise during the processing of C# statements and expressions cause an exception in certain circumstances when the operation cannot be completed normally. [Example: An integer division operation (§14.7.2) throws a System.DivideByZeroException if the denominator is zero. end example] See §23.4 for a list of the various exceptions that can occur in this way.