Section 12.5. .NET Exception Hierarchy


12.5. .NET Exception Hierarchy

In Visual Basic, the exception-handling mechanism allows only objects of class Exception (namespace System) and its derived classes to be thrown and caught. Visual Basic programs may interact with software components written in other .NET languages (such as C++) that do not restrict exception types. Such exceptions are wrapped by the CLR as Exception objects, so they can be caught by a Catch clause of type Exception.

This section overviews several of the .NET Framework's exception classes and focuses exclusively on exceptions that derive from class Exception. In addition, we discuss how to determine whether a particular method throws exceptions.

12.5.1. Classes ApplicationException and SystemException

Class Exception of namespace System is the base class of the .NET Framework exception class hierarchy. Two of the most important classes derived from Exception are ApplicationException and SystemException. ApplicationException is a base class that you can extend to create exception classes that are specific to their applications. We show how to create user-defined exception classes in Section 12.8. Programs can recover from most ApplicationExceptions and continue execution.

The CLR generates SystemExceptions, which can occur at any point during program execution. Many of these exceptions can be avoided if applications are coded properly. For example, if a program attempts to access an out-of-range array index, the CLR throws an exception of type IndexOutOfRangeException (a derived class of SystemException). Similarly, an exception occurs when a program uses an object reference to manipulate an object that does not yet exist (i.e., the reference has the value Nothing). Attempting to use a Nothing reference causes a NullReferenceException (another derived class of SystemException). You saw earlier in this chapter that a DivideByZeroException occurs in integer division when a program attempts to divide by zero.

Other SystemException types thrown by the CLR include OutOfMemoryException, StackOverflowException and ExecutionEngineException. These are thrown when the something goes wrong that causes the CLR to become unstable. In some cases, such exceptions cannot even be caught. In general, it is best to simply log such exceptions (possibly by writing information about the problem to a file) then terminate your application.

A benefit of the exception class hierarchy is that a Catch block can catch exceptions of a particular type orbecause of the is-a relationship of inheritancecan use a base-class type to catch exceptions in a hierarchy of related exception types. For example, Section 12.4.2 discussed the Catch block with no parameter, which catches exceptions of all types (including those that are not derived from Exception). A Catch block that specifies a parameter of type Exception can catch all exceptions that derive from Exception, because Exception is the base class of all exception classes. The advantage of this approach is that the exception handler can access the caught exception's information via the parameter in the Catch. We demonstrated accessing the information in an exception in line 29 of Fig. 12.2. We'll say more about accessing exception information in Section 12.7.

Using inheritance with exceptions enables a Catch block to catch related exceptions using a concise notation. A set of exception handlers could catch each derived-class exception type individually, but catching the base-class exception type is more concise. However, this technique makes sense only if the handling behavior is the same for a base class and all of its derived classes. Otherwise, catch each derived-class exception individually.

Common Programming Error 12.3

It is a compilation error if a Catch block that catches a base-class exception is placed before a Catch block for any of that class's derived-class types. If this were allowed, the base-class Catch block would catch all base-class and derived-class exceptions, so the derived-class exception handler would never execute.


12.5.2. Determining Which Exceptions a Method Throws

How do we determine that an exception might occur in a program? For methods of the .NET Framework classes, read the detailed descriptions of the methods in the online documentation (accessible through the IDE's Help menu). If a method throws an exception, its description contains a section called Exceptions that specifies the types of exceptions the method throws and briefly describes possible causes for the exceptions. For example, search for "Convert.ToInt32 method" in the Index of the Visual Studio online documentation (use the .NET Framework filter). Select the document entitled Convert.ToInt32 Method. In the document that describes the method, click the link Convert.ToInt32(String). In the document that appears, the Exceptions section (near the bottom of the document) indicates that method Convert.ToInt32 throws two exception typesFormatException and OverflowExceptionand describes the reason why each might occur.

Software Engineering Observation 12.1

If a method throws exceptions, statements that invoke the method directly or indirectly should be placed in TRy blocks, and those exceptions should be caught and handled.


It is more difficult to determine when the CLR throws exceptions. Such information appears in the Visual Basic Language Specification 8.0 (http://go.microsoft.com/fwlink/?LinkId=62990). This document defines Visual Basic's syntax and specifies cases in which exceptions are thrown. Figure 12.2 demonstrated that the CLR throws a DivideByZeroException in integer arithmetic when a program attempts to divide by zero. Section 11.13.6 of the language specification discusses the division operator and when DivideByZeroExceptions occur.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net