Title: Chapter 13 Exception Handling (????)
1Chapter 13 Exception Handling
(????)
- 13.1 Introduction
- 13.2 Exception Handling Mechanism
- 13.3 Throwing Mechanisms
- 13.4 Catching Mechanisms
- 13.5 Rethrowing an Exception
- 13.6 Specifying Exception
213.1 Introduction
- A program to read in two integers and displays
their quotient
Quotient
Run
int main() cout ltlt "Enter two integers "
int num1, num2 cin gtgt num1 gtgt num2 cout
ltltnum1ltlt"/"ltltnum2ltlt" is " ltlt (num1 / num2) ltlt
endl return 0
If you enter 0 for num2, then?
A runtime error! An exception!
3Whats Exception?
- Peculiar problems other than logic or syntax
errors - Run time anomalies or unusual conditions
- Usually cause the program terminate abnormally
- Including
- division by zero,
- access to an array outside of its bounds, or
- running out of memory or disk space,
- etc.
4Naive Exception Handling
- Example of Integer Division
- Adding an if statement to test the second number
if (num2 ! 0) cout ltltnum1 ltlt" / "ltltnum2ltlt"
is " ltlt (num1 / num2) ltlt endl else
cout ltlt "Divisor cannot be zero"ltltendl
QuotientWithIf
Run
513.2 Exception Handling Mechanism
- Special Mechanism to detect, report and act
against exceptions. - Four tasks
- Find the problem(Hit the exception)
- Inform that an error has occurred (Throw the
exception) - Receive the error information (Catch the
exception) - Take corrective actions (Handle the exception)
6Try-throw-catch
- The try block
- To preface a block of statements which may
generate exceptions. - The throw statement
- To report the exception by throwing a object.
- The catch block
- To catche the exception thrown and take actions
to handle it.
try block
Detects and throws an exception
Exception object
catch block
Catches and handles the exception
7C Exception Handling
int main() cout ltlt"Enter two integers "
int num1, num2 cin gtgt num1 gtgt num2 try
if (num2 0) throw num1 cout ltlt num1 ltlt
" / " ltlt num2 ltlt " is " ltlt (num1 /
num2) ltlt endl catch (int e) cout ltlt
"Exception an integer " ltlt e ltlt "
cannot be divided by zero" ltlt endl cout ltlt
"Execution continues ..." ltlt endl
QuotientWithException
Run
Exception Handler
8Template for try-throw-catch
- try
-
- Code to try
- Throw an exception by throw or calling
- More code to try
-
- catch (type e)
-
- Code to process the exception
try // ... catch (type) //
The rest code will NOT be executed if the
exception occurs!
General
Parameter Omitted
Program 13.1.cpp.html
9Advantages of C Exception Handling
int quotient(int num1, int num2) if (num2 !
0) return num1 / num2 else
return -1//?
int quotient(int num1, int num2) if (num2
0) throw num1 return num1 /
num2
C Exception Handling
Naive Exception Handling
- More powerful
- - Can handle various cases
- More structural
- - Separate normal code and exception handling
code
10When to Use Exceptions
- Yes!
- Common exceptions that may occur in multiple
classes - Unexpected error conditions
May be difficult to determine.
DO NOT abuse exception handling!
- NO!
- Simple errors that may occur in individual
functions - Simple, expected situations
1113.3 Throwing Mechanism
Throw point
Function that causes an exception
- Exception usually is thrown by a function invoked
inside a try block. - Throw point
- The point at which the throw is executed
- Upon exception
- The control will jump to the catch block, and
- Cannot return back the throw point.
Invoke function
try block
Invokes a function that contains an exception
Throw exception
catch block
Catches and handles the exception
12Exception from a Function
- A program to compute quotient using a function
int quotient(int num1, int num2) if (num2
0) throw num1 return num1 / num2 int
main() // try int result
quotient(num1, num2) // catch
(int e) // //
QuotientWithFunction
Run
13Exception Classes
14Using Standard Classes
- bad_alloc
- bad_cast
- runtime_error
ExceptionDemo1
Run
ExceptionDemo2
Run
ExceptionDemo3
Run
15Customed Exception Classes
- Use standard classes whenever possible
- Create yours if necessary
- An exception class is just like any C class
- It is often desirable to derive it from exception
- Directly or indirectly
- To utilize the common features (e.g., the what()
function) in the exception class.
16The Triangle Exception Class
TriangleException.h
Triangle.h
TestTriangle.cpp
Run
1713.4 Catching Mechanism
- More than one exception in one try block?
- One catch block can handle only one exception
try catch (E e)
try catch (E1 e)
catch (E2 e) catch (E3
e)
Program 13.3.cpp.html
18Example of Multiple Catches
- Another type of exception in Triangle
class NonPositiveSideException public
logic_error public NonPositiveSideException(
double side) logic_error("Non-posit
ive side") this-gtside side
double getSide() return side
private double side
NewTriangle.h
NonPositiveSideException.h
MultipleCatchDemo
Run
19Notes for Multiple Catches
- If a catch block catches exception objects of a
base class, it can catch all the exception
objects of the derived classes of that base
class. - So, a catch block for a base class type should
appear after a catch block for a derived class
type.
2014.5 Rethrowing an Exception
- An exception can be rethrown
- By a handler
- For two reasons
- The current handler cannot process the exception
or - The current handler wants to let its caller be
notified of the exception. - The syntax
try statements catch (TheException ex)
perform operations before exits throw
Program 13.5.cpp.html
21Exception Propagation
22Exception Propagation
- An exception that cant be handled by any of a
functions catch blocks is thrown to the caller - The exception is propagated along the reverse
order of the function calling
2314.6 Specifying Exception
- What try-catch block should be provided in a
function? - Exceptions thrown by
- The function itself
- The functions called
- What exceptions may be thrown by a function?
24Exception Specification
- Also known as throw list
- lists exceptions that a function can throw
returnType functionName(parameterList)
throw (exceptionList)
Program 13.6.cpp.html
25Two Special Lists
- No throw list
- May throw any exception!
- Empty throw list
- Cant throw any exception!
returnType functionName(parameterList)
returnType functionName(parameterList) throw ()
26Undeclared Exception
- Throwing an exception not declared in the throw
list? - The function unexpected will be invoked!
- A standard C function
- Normally terminates the program
Visual studio doesnt satisfy the throw list
specification well.
27Catch All Types of Exceptions
- The exception type may not be able to anticipate
- To force a catch block to catch all exceptions
thrown
catch() //Statements for processing
//all exceptions
Program 13.4.cpp.html
27
28A Summary
- Various exceptions may occur
- Standard exception classes
- Customized exception classes
- try-throw-catch
- Multiple catches
- Propagation of exceptions
- Rethrow
- Exception specification