2006 Pearson Education, Inc. All rights reserved Exception Handling
2006 Pearson Education, Inc. All rights reserved. 2 It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something. — Franklin Delano Roosevelt O! throw away the worser part of it, And live the purer with the other half. — William Shakespeare
2006 Pearson Education, Inc. All rights reserved. 3 And oftentimes excusing of a fault Doth make the fault the worse by the excuse. — William Shakespeare If they’re running and they don’t look where they’re going I have to come out from somewhere and catch them. — J. D. Salinger O infinite virtue! com’st thou smiling from the world’s great snare uncaught? — William Shakespeare
2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn: What exceptions are and how they are handled. When to use exception handling. To use try blocks to delimit code in which exceptions might occur. To throw exceptions to indicate a problem. To use catch blocks to specify exception handlers. To use the finally block to release resources. The.NET exception class hierarchy. Exception properties. To create user-defined exceptions.
2006 Pearson Education, Inc. All rights reserved Introduction 12.2 Exception Handling Overview 12.3 Example: Divide by Zero Without Exception Handling 12.4 Example: Handling DivideByZeroException s and FormatException s Enclosing Code in a try Block Catching Exceptions Uncaught Exceptions Termination Model of Exception Handling Flow of Control When Exceptions Occur 12.5.NET Exception Hierarchy Classes ApplicationException and SystemException Determining Which Exceptions a Method Throws 12.6 finally Block 12.7 Exception Properties 12.8 User-Defined Exception Classes 12.9 Wrap-Up
2006 Pearson Education, Inc. All rights reserved. 6 Error-Prevention Tip 12.1 Exception handling helps improve a program’s fault tolerance.
2006 Pearson Education, Inc. All rights reserved. 7 Outline DivideByZeroNoExce ptionHandling.cs (1 of 2)
2006 Pearson Education, Inc. All rights reserved. 8 Outline DivideByZeroNoExce ptionHandling.cs (2 of 2)
2006 Pearson Education, Inc. All rights reserved. 9 Outline DivideByZeroTest.cs (1 of 3)
2006 Pearson Education, Inc. All rights reserved. 10 Outline DivideByZeroTest.cs (2 of 3)
2006 Pearson Education, Inc. All rights reserved. 11 Outline DivideByZeroTest.cs (3 of 3) (a) (b) (d) (c) (e)
2006 Pearson Education, Inc. All rights reserved. 12 Fig | Exception Assistant. Throw point Exception Assistant
2006 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 12.1 Logic errors can occur if you assume that after an exception is handled, control will return to the first statement after the throw point.
2006 Pearson Education, Inc. All rights reserved. 14 Common Programming Error 12.2 Specifying a comma-separated list of parameters in a catch block is a syntax error. A catch block can have at most one parameter.
2006 Pearson Education, Inc. All rights reserved. 15 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.
2006 Pearson Education, Inc. All rights reserved. 16 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.
2006 Pearson Education, Inc. All rights reserved. 17 Error-Prevention Tip 12.2 The CLR does not completely eliminate memory leaks. The CLR will not garbage collect an object until the program contains no more references to that object. Thus, memory leaks can occur if programmers inadvertently keep references to unwanted objects.
2006 Pearson Education, Inc. All rights reserved. 18 Error-Prevention Tip 12.3 A finally block typically contains code to release resources acquired in the corresponding try block, which makes the finally block an effective mechanism for eliminating resource leaks.
2006 Pearson Education, Inc. All rights reserved. 19 Performance Tip 12.1 As a rule, resources should be released as soon as they are no longer needed in a program. This makes them available for reuse promptly.
2006 Pearson Education, Inc. All rights reserved. 20 Common Programming Error 12.4 Placing the finally block before a catch block is a syntax error.
2006 Pearson Education, Inc. All rights reserved. 21 Outline UsingExceptions.cs (1 of 8)
2006 Pearson Education, Inc. All rights reserved. 22 Outline UsingExceptions.cs (2 of 8)
2006 Pearson Education, Inc. All rights reserved. 23 Outline UsingExceptions.cs (3 of 8)
2006 Pearson Education, Inc. All rights reserved. 24 Outline UsingExceptions.cs (4 of 8)
2006 Pearson Education, Inc. All rights reserved. 25 Outline UsingExceptions.cs (5 of 8)
2006 Pearson Education, Inc. All rights reserved. 26 Outline UsingExceptions.cs (6 of 8)
2006 Pearson Education, Inc. All rights reserved. 27 Outline UsingExceptions.cs (7 of 8)
2006 Pearson Education, Inc. All rights reserved. 28 Outline UsingExceptions.cs (8 of 8)
2006 Pearson Education, Inc. All rights reserved. 29 Common Programming Error 12.5 It is a compilation error if the argument of a throw —an exception object—is not of class Exception or one of its derived classes.
2006 Pearson Education, Inc. All rights reserved. 30 Common Programming Error 12.6 Throwing an exception from a finally block can be dangerous. If an uncaught exception is awaiting processing when the finally block executes, and the finally block throws a new exception that is not caught in the finally block, the first exception is lost, and the new exception is passed to the next enclosing try block.
2006 Pearson Education, Inc. All rights reserved. 31 Error-Prevention Tip 12.4 When placing code that can throw an exception in a finally block, always enclose the code in a try statement that catches the appropriate exception types. This prevents the loss of any uncaught and rethrown exceptions that occur before the finally block executes.
2006 Pearson Education, Inc. All rights reserved. 32 Software Engineering Observation 12.2 Do not place try blocks around every statement that might throw an exception, because this can make programs difficult to read. It is better to place one try block around a significant portion of code, and follow this try block with catch blocks that handle each of the possible exceptions. Then follow the catch blocks with a single finally block. Separate try blocks should be used when it is important to distinguish between multiple statements that can throw the same exception type.
2006 Pearson Education, Inc. All rights reserved. 33 Error-Prevention Tip 12.5 A stack trace shows the complete method-call stack at the time an exception occurred. This enables the programmer to view the series of method calls that led to the exception. Information in the stack trace includes the names of the methods on the call stack at the time of the exception, the names of the classes in which the methods are defined and the names of the namespaces in which the classes are defined. If the program database (PDB) file that contains the debugging information for the method is available, the stack trace also includes line numbers; the first line number indicates the throw point, and subsequent line numbers indicate the locations from which the methods in the stack trace were called. PDB files are created by the IDE to maintain the debugging information for your projects.
2006 Pearson Education, Inc. All rights reserved. 34 Outline Properties.cs (1 of 4)
2006 Pearson Education, Inc. All rights reserved. 35 Outline Properties.cs (2 of 4)
2006 Pearson Education, Inc. All rights reserved. 36 Outline Properties.cs (3 of 4)
2006 Pearson Education, Inc. All rights reserved. 37 Outline Properties.cs (4 of 4)
2006 Pearson Education, Inc. All rights reserved. 38 Error-Prevention Tip 12.6 When catching and rethrowing an exception, provide additional debugging information in the rethrown exception. To do so, create an Exception object containing more specific debugging information, then pass the original caught exception to the new exception object’s constructor to initialize the InnerException property.
2006 Pearson Education, Inc. All rights reserved. 39 Good Programming Practice 12.1 Associating each type of malfunction with an appropriately named exception class improves program clarity.
2006 Pearson Education, Inc. All rights reserved. 40 Software Engineering Observation 12.3 Before creating a user-defined exception class, investigate the existing exceptions in the.NET Framework Class Library to determine whether an appropriate exception type already exists.
2006 Pearson Education, Inc. All rights reserved. 41 Outline NegativeNumberExce ption.cs (1 of 2)
2006 Pearson Education, Inc. All rights reserved. 42 Outline NegativeNumberExce ption.cs (2 of 2)
2006 Pearson Education, Inc. All rights reserved. 43 Outline SquareRootTest.cs (1 of 3)
2006 Pearson Education, Inc. All rights reserved. 44 Outline SquareRootTest.cs (2 of 3)
2006 Pearson Education, Inc. All rights reserved. 45 Outline SquareRootTest.cs (3 of 3) (a) (c) (b) (d)