Next:
Other Error-Handling Techniques
Up:
Exception Handling
Previous:
Introduction
Contents
Exception-Handling Overview
Consider pseudocode
Perform a task
If the preceding task did not execute correctly
Perform error processing
Perform next task
If the preceding task did not execute correctly
Perform error processing
Mixing logic and error handling
Can make program difficult to read/debug
Exception handling removes error correction from
main line
of program
Exception handling
For synchronous errors (divide by zero, null pointer)
Cannot handle asynchronous errors (independent of program)
Disk I/O, mouse, keyboard, network messages
Easy to handle errors
Terminology
Function that has error
throws an exception
Exception handler
(if it exists) can deal with problem;
Catches
and
handles
exception
If no exception handler,
uncaught
exception; Could terminate program
C++ code
try{ code that may raise exception } catch(exceptionType){ code to handle exception }
try
block encloses code that may raise exception
One or more
catch
blocks follow
Catch and handle exception, if appropriate
Take parameter; if named, can access exception object
Throw point
Location in
try
block where exception occurred
If exception handled
Program skips remainder of
try
block
Resumes after
catch
blocks
If not handled
Function terminates
Looks for enclosing
catch
block (stack unwinding, 13.8)
If no exception
Program skips
catch
blocks
Next:
Other Error-Handling Techniques
Up:
Exception Handling
Previous:
Introduction
Contents
2004-12-28