Week6_ExceptionHandling
Week6_ExceptionHandling
Carnegie Mellon
Outline
What exceptions are and when to use them
Using try, catch and throw to detect, handle and
indicate exceptions, respectively
To process uncaught and unexpected exceptions
To declare new exception classes
How stack unwinding enables exceptions not
caught in one scope to be caught in another
scope
To handle new failures
To understand the standard exception hierarchy
Introduction
Exceptions
Indicate problems that occur during a program’s
execution
Occur infrequently
Exception handling
Can resolve exceptions
Allow a program to continue executing or
Notify the user of the problem and
Terminate the program in a controlled manner
Makes programs robust and fault-tolerant
Fundamental Philosophy
Mechanism for sending an exception signal up
the call stack
Regardless of intervening calls
Class exception
The standard C++ base class for all exceptions
Provides derived classes with virtual function
what()
Returns the exception’s stored error message
example
30
31 // enable user to enter two integers to divide
– (2 of 2)
32 while ( cin >> number1 >> number2 )
33 {
34 // try block contains code that might throw exception
35 // and code that should not execute if an exception occurs
36 try
37 {
38 result = quotient( number1, number2 );
39 cout << "The quotient is: " << result << endl;
40 } // end try
41
42 // exception handler handles a divide-by-zero exception
43 catch ( DivideByZeroException ÷ByZeroException )
44 {
45 cout << "Exception occurred: "
46 << divideByZeroException.what() << endl;
47 } // end catch
48
49 cout << "\nEnter two integers (end-of-file to end): ";
50 } // end while
51
52 cout << endl;
53 return 0; // terminate normally
54 } // end main
Questions?
try Blocks
Keyword try followed by curly braces "{}“
Should enclose
Statements that might cause exceptions
Statements that should be skipped in case of an
exception
Catch Handlers
Immediately follow a try block
One or more catch handlers for each try block
Keyword catch
Exception parameter enclosed in parentheses
Represents the type of exception to process
Can provide an optional parameter name to interact
with the caught exception object
Executes if exception parameter type matches
the exception thrown in the try block
Could be a base class of the thrown exception’s class
Stack “Unwinding”
Occurs when a thrown exception is not caught in
a particular scope
Unwinding a Function terminates that function
All local variables of the function are destroyed
Invokes destructors
Control returns to point where function was invoked
Attempts are made to catch the exception in
outer try…catch blocks
If the exception is never caught, the function
terminate() is called
Observations
With exception handling, program can continue
executing after dealing with a problem
rather than terminating
Throwing an Exception
Use keyword throw followed by an operand
representing the type of exception
The throw operand can be of any type
If the throw operand is an object, it is called an
exception object
The throw operand initializes the exception
parameter in the matching catch handler, if one
is found
Notes
Catching an exception object by reference
eliminates the overhead of copying the object
that represents the thrown exception
Re-throwing an Exception
Empty throw; statement
Exception Specifications
Also called throw lists
Keyword throw Optional!
Comma-separated list of exception classes in
parentheses
Example
int someFunction( double value )
throw ( ExceptionA, ExceptionB,
ExceptionC )
{
...
}
Indicates someFunction can throw types ExceptionA,
ExceptionB and ExceptionC
CS-2303, A-Term 2012 Exception Handling in C++ 30
Worcester Polytechnic Institute
Carnegie Mellon
Error Note
Compiler will not generate compilation error if
Function contains a throw expression for an exception
not listed in exception specification
Error occurs only when that function attempts to
throw that exception
At run time
To avoid surprises at execution time, carefully
check your code to ensure that functions do not
throw exceptions not listed in their exception
specifications
Questions?
Note
When an exception is thrown from the
constructor for an object that is created in a new
expression, …
Questions?