CS 202 Fall 2013 > Notes for Tuesday, October 29, 2013 |
CS 202 Fall 2013
Notes for Tuesday, October 29, 2013
Exceptions [16.1]
Background: Error Handling
An error condition is a condition occurring while a program is running, that cannot be handled by the normal flow of execution. Some examples:
- The code divides two numbers, and the denominator is zero.
- An attempt is made to read from a file, but the file does not exist.
- Item 1000 is read from a list
(e.g., a
vector
), but the list is only 100 items long.
If a function cannot handle an error condition itself, then it must signal its caller somehow.
- It can return a special error value.
- It can set a flag to be checked by a separate function.
Both of the above are common. Note that the second strategy is that used by C++ streams.
These strategies have been found to be somewhat unsatisfactory. For example, neither allows for effective error handling if an error occurs in the middle of an expression (think about division by zero in the middle of a complicated arithmetic expression) or if an error occurs in a constructor. Therefore, a third method has been developed.
- Throw an exception.
Introduction to Exceptions
Consider the following code.
[C++]
void printHappy(int n) { if (n == 5) throw logic_error("I don't like the given number."); for (int i = 0; i < n; ++i) { cout << "Happy" << endl; } } int main() { try { foo(6); } catch (logic_error & e) { cout << "ERROR" << endl; cout << "Message: " << e.what() << endl; } cout << endl; cout << "Done!" << endl; }
An exception is an object that is thrown from a block of code, generally to indicate an error condition. When this happens, normal execution stops, destructors for local variables are called, and then a search is made for the nearest enclosing try-block with a catch-block whose type matches that of the exception. If one is found, then the catch-block is executed; otherwise, the program is terminated.
For example, above, calling function foo
with
parameter 6
produces no error.
There is no throw.
The word “Happy
” will be printed six times.
Then the program will print “Done!
”
and exit.
But suppose “foo(6)
”
is changed to “foo(5)
”.
Then an object of type logic_error
is thrown.
Function printHappy
exits
(“Happy
” is never printed),
and a search is made for an enclosing try
with an associated catch
that will take a
logic_error
.
One is found, and the catch-block is executed.
In the standard exception classes (which include logic_error
)
the string passed to the constructor is returned by member function
what
.
Thus, the above would print.
ERROR Message: I don't like the given number. Done!
Note that normal execution resumes after the catch-block.
Also note that the catch
statement takes
the exception object by reference.
This can also be done by value
(just remove the “&
”),
but doing it by reference is almost always better.
Catch exceptions by reference!
See
excep.cpp
for a program that does error handling using exceptions.
For today’s lab work, see the 10/29 Challenge.