Tuesday, January 18, 2011

Uncaught exceptions, catch-all handlers, and exception specifiers

By now, you should have a reasonable idea of how exceptions work. In this lesson, we’ll cover a few more interesting exception cases.
Uncaught exceptions:
In the past few examples, there are quite a few cases where a function assumes its caller (or another function somewhere up the call stack) will handle the exception. In the following example, MySqrt() assumes someone will handle the exception that it throws — but what happens if nobody actually does?
Here’s our square root program again, minus the try block in main():
01#include "math.h" // for sqrt() function
02using namespace std;
03
04// A modular square root function
05double MySqrt(double dX)
06{
07    // If the user entered a negative number, this is an error condition
08    if (dX < 0.0)
09        throw "Can not take sqrt of negative number"; // throw exception of type char*
10
11    return sqrt(dX);
12}
13
14int main()
15{
16    cout << "Enter a number: ";
17    double dX;
18    cin >> dX;
19
20    // Look ma, no exception handler!
21    cout << "The sqrt of " << dX << " is " << MySqrt(dX) << endl;
22}
Now, let’s say the user enters -4, and MySqrt(-4) raises an exception. MySqrt() doesn’t handle the exception, so the program stack unwinds and control returns to main(). But there’s no exception handler here either, so main() terminates. At this point, we just terminated our application!
When main() terminates with an unhandled exception, the operating system will generally notify you that an unhandled exception error has occurred. How it does this depends on the operating system, but possibilities include printing an error message, popping up an error dialog, or simply crashing. Some OS’s are less graceful than others. Generally this is something you want to avoid altogether!
Catch-all handlers
And now we find ourselves in a condundrum: functions can potentially throw exceptions of any data type, and if an exception is not caught, it will propagate to the top of your program and cause it to terminate. Since it’s possible to call functions without knowing how they are even implemented, how can we possibly prevent this from happening?
Fortunately, C++ provides us with a mechanism to catch all types of exceptions. This is known as a catch-all handler. A catch-all handler works just like a normal catch block, except that instead of using a specific type to catch, it uses the ellipses operator (…) as the type to catch. If you recall from lesson 7.14 on ellipses and why to avoid them, ellipses were previously used to pass arguments of any type to a function. In this context, they represent exceptions of any data type. Here’s an simple example:
01try
02{
03    throw 5; // throw an int exception
04}
05catch (double dX)
06{
07    cout << "We caught an exception of type double: " << dX << endl;
08}
09catch (...) // catch-all handler
10{
11    cout << "We caught an exception of an undetermined type" << endl;
12}
Because there is no specific exception handler for type int, the catch-all handler catches this exception. This example produces the following result:
We caught an exception of an undetermined type
The catch-all handler should be placed last in the catch block chain. This is to ensure that exceptions can be caught by exception handlers tailored to specific data types if those handlers exist. Visual Studio enforces this constraint — I am unsure if other compilers do.
Often, the catch-all handler block is left empty:
1catch(...) {} // ignore any unanticipated exceptions
This will catch any unanticipated exceptions and prevent them from stack unwinding to the top of your program, but does no specific error handling.
Using the catch-all handler to wrap main()
One interesting use for the catch-all handler is to wrap the contents of main():
01int main()
02{
03
04    try
05    {
06        RunGame();
07    }
08    catch(...)
09    {
10        cerr << "Abnormal termination" << endl;
11    }
12
13    SaveState(); // Save user's game
14    return 1;
15}
In this case, if RunGame() or any of the functions it calls throws an exception that is not caught, that exception will unwind up the stack and eventually get caught by this catch-all handler. This will prevent main() from terminating, and gives us a chance to print an error of our choosing and then save the user’s state before exiting. This can be useful to catch and handle problems that may be unanticipated.
Exception specifiers
This subsection should be considered optional reading because exception specifiers are rarely used in practice, are not well supported by compilers, and Bjarne Stroustrup (the creator of C++) considers them a failed experiment.
Exception specifiers are a mechanism that allows us to use a function declaration to specify whether a function may or will not throw exceptions. This can be useful in determining whether a function call needs to be put inside a try block or not.
There are three types of exception specifiers, all of which use what is called the throw (…) syntax.
First, we can use an empty throw statement to denote that a function does not throw any exceptions outside of itself:
1int DoSomething() throw(); // does not throw exceptions
Note that DoSomething() can still use exceptions as long as they are handled internally. Any function that is declared with throw() is supposed to cause the program to terminate immediately if it does try to throw an exception outside of itself, but implementation is spotty.
Second, we can use a specific throw statement to denote that a function may throw a particular type of exception:
1int DoSomething() throw(double); // may throw a double
Finally, we can use a catch-all throw statement to denote that a function may throw an unspecified type of exception:
1int DoSomething() throw(...); // may throw anything
Due to the incomplete compiler implementation, the fact that exception specifiers are more like statements of intent than guarantees, some incompatibility with template functions, and the fact that most C++ programmers are unaware of their existence, I recommend you do not bother using exception specifiers.

No comments: