Week9_ExceptionHandling
Week9_ExceptionHandling
HANDLING EXCEPTIONS IN C+
+
Dr.Surendran,
Assistant Professor,
College of Computer Studies
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
• Programs can
• Recover from exceptions
• Hide exceptions
• Pass exceptions up the “chain of
command”
• Ignore certain exceptions and let
someone else handle them
• 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
30 Zero Divide
31 // enable user to enter two integers to divide
32
33
while ( cin >> number1 >> number2 )
{
Example
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
try Blocks
• Keyword try followed by 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
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
Exception Specifications
• Also called throw lists
• Keyword throw
• Comma-separated list of exception classes in
parentheses
Optional!
• Example
• int someFunction( double value )
throw ( ExceptionA, ExceptionB,
ExceptionC )
{
...
}
• Indicates someFunction can throw types
ExceptionA, ExceptionB and ExceptionC
Error Note
• The compiler will not generate a compilation error
if a function contains a throw expression for an
exception not listed in the function’s 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
Exception Hierarchies
class Matherr { };
class Overflow: public Matherr { };
class Underflow: public Matherr { };
class Zerodivide: public Matherr { };
//…
void g { }
{
try {
f();
}
catch (Overflow) { } // handle overflow, derived exceptions
catch (Matherr) { } // handle any Matherr that’s not
overflow
}