3
 2000 Prentice Hall, Inc. All rights reserved. 23.1Introduction (II) Exception handling - catch errors before they occur –Deals with synchronous errors (i.E., Divide by zero) –Does not deal with asynchronous errors - disk I/O completions, mouse clicks - use interrupt processing –Used when system can recover from error Exception handler - recovery procedure –Typically used when error dealt with in different place than where it occurred –Useful when program cannot recover but must shut down cleanly Exception handling should not be used for program control –Not optimized, can harm program performance

4
 2000 Prentice Hall, Inc. All rights reserved. 23.1Introduction (III) Exception handling improves fault-tolerance –Easier to write error-processing code –Specify what type of exceptions are to be caught Most programs support only single threads –Techniques in this chapter apply for multithreaded OS as well (windows NT, OS/2, some UNIX) Exception handling another way to return control from a function or block of code

5
 2000 Prentice Hall, Inc. All rights reserved. 23.2When Exception Handling Should Be Used Error handling should be used for –Processing exceptional situations –Processing exceptions for components that cannot handle them directly –Processing exceptions for widely used components (libraries, classes, functions) that should not process their own exceptions –Large projects that require uniform error processing

6
 2000 Prentice Hall, Inc. All rights reserved. 23.3Other Error-Handling Techniques Use assert –If assertion false, the program terminates Ignore exceptions –Use this "technique" on casual, personal programs - not commercial! Abort the program –Appropriate for nonfatal errors give appearance that program functioned correctly –Inappropriate for mission-critical programs, can cause resource leaks Set some error indicator –Program may not check indicator at all points there error could occur

9
 2000 Prentice Hall, Inc. All rights reserved. 23.4Basics of C++ Exception Handling: try, throw, catch (II) Format –Enclose code that may have an error in try block –Follow with one or more catch blocks Each catch block has an exception handler –If exception occurs and matches parameter in catch block, code in catch block executed –If no exception thrown, exception handlers skipped and control resumes after catch blocks –throw point - place where exception occurred Control cannot return to throw point

17
 2000 Prentice Hall, Inc. All rights reserved. 23.7Catching an Exception (II) Catch all exceptions catch(...) - catches all exceptions You do not know what type of exception occurred There is no parameter name - cannot reference the object If no handler matches thrown object –Searches next enclosing try block If none found, terminate called –If found, control resumes after last catch block –If several handlers match thrown object, first one found is executed

19
 2000 Prentice Hall, Inc. All rights reserved. 23.7Catching an Exception (IV) Unreleased resources –Resources may have been allocated when exception thrown –catch handler should delete space allocated by new and close any opened files catch handlers can throw exceptions –Exceptions can only be processed by outer try blocks

20
 2000 Prentice Hall, Inc. All rights reserved. 23.8Rethrowing an Exception Rethrowing exceptions –Used when an exception handler cannot process an exception –Rethrow exception with the statement: throw; No arguments If no exception thrown in first place, calls terminate –Handler can always rethrow exception, even if it performed some processing –Rethrown exception detected by next enclosing try block

25
 2000 Prentice Hall, Inc. All rights reserved Stack Unwinding Function-call stack unwound when exception thrown and not caught in a particular scope –Tries to catch exception in next outer try/catch block –Function in which exception was not caught terminates Local variables destroyed Control returns to place where function was called –If control returns to a try block, attempt made to catch exception Otherwise, further unwinds stack –If exception not caught, terminate called

26
 2000 Prentice Hall, Inc. All rights reserved Constructors, Destructors and Exception Handling What to do with an error in a constructor? –A constructor cannot return a value - how do we let the outside world know of an error? Keep defective object and hope someone tests it Set some variable outside constructor –A thrown exception can tell outside world about a failed constructor –catch handler must have a copy constructor for thrown object

27
 2000 Prentice Hall, Inc. All rights reserved Constructors, Destructors and Exception Handling (II) Thrown exceptions in constructors –Destructors called for all completed base-class objects and member objects before exception thrown –If the destructor that is originally called due to stack unwinding ends up throwing an exception, terminate called –If object has partially completed member objects when exception thrown, destructors called for completed objects

29
 2000 Prentice Hall, Inc. All rights reserved Exceptions and Inheritance Exception classes can be derived from base classes If catch can get a pointer/reference to a base class, can also catch pointers/references to derived classes

30
 2000 Prentice Hall, Inc. All rights reserved Processing new Failures If new could not allocate memory –Old method - use assert function If new returns 0, abort Does not allow program to recover –Modern method (header ) new throws bad_alloc exception –Method used depends on compiler –On some compilers: use new(nothrow) instead of new to have new return 0 when it fails Function set_new_handler( functionName ) - sets which function is called when new fails. Function can return no value and take no arguments new will not throw bad_alloc

31
 2000 Prentice Hall, Inc. All rights reserved Processing new Failures (II) new –Loop that tries to acquire memory A new handler function should either: –Make more memory available by deleting other dynamically allocated memory and return to the loop in operator new –Throw an exception of type bad_alloc –Call function abort or exit (header ) to terminate the program