7.exception Handling
7.exception Handling
7.exception Handling
Lecture 8 Exception
Handling (C++)
CS162 Object Oriented Programming
Lecture adapted from Imran Ihsan
C++ How to Program (9th Edition) www.imranihsan.com
By Paul Deitel and Harvey Deitel
Prentice Hall, Inc. All rights reserved. OOP 08 - Exception
imranihsan.com
Handling
+ 8.1 Introduction
Exceptions
Indicates problem occurred in program
Not common
An "exception" to a program that usually works
Exception Handling
Resolve exceptions
Program may be able to continue
Controlled termination
Write fault-tolerant programs
As an example, we will handle a divide-by-zero error
1
5/14/2014
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
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
2
5/14/2014
C++ code
try {
code that may raise exception
}
catch (exceptionType){
code to handle exception
}
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
3
5/14/2014
Ignore exception
Typical for personal (not commercial) software
Program may fail
Abort program
Usually appropriate
Not appropriate for mission-critical software
4
5/14/2014
Exception objects
Base class exception ( <exception> )
Constructor can take a string (to describe exception)
Member function what() returns that string
In division function
Test denominator
If zero, throw exception (throw object)
In try block
Attempt to divide
Have enclosing catch block
Catch DivideByZeroException objects
5
5/14/2014
6
5/14/2014
+
Enter two integers (end-of-file to end): 100 7
The quotient is: 14.2857
7
5/14/2014
Rethrowing exceptions
Use when exception handler cannot process exception
Can still rethrow if handler did some processing
Can rethrow exception to another handler
Goes to next enclosing try block
Corresponding catch blocks try to handle
To rethrow
Use statement "throw;"
No arguments
Terminates function
8
5/14/2014
+ 30
31 cout << "This also should not print\n";
32
33 } // end function throwException
34
35 int main()
36 {
37 // throw exception
38 try {
39 cout << "\nmain invokes function throwException\n";
40 throwException();
41 cout << "This should not print\n";
42
43 } // end try
44
45 // handle exception
46 catch ( exception &caughtException ) {
47 cout << "\n\nException handled in main\n";
48
49 } // end catch
50
51 cout << "Program control continues after catch in main\n";
52
53 return 0;
54
55 } // end main