Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
There are two basic models in exception-handling theory: termination and resumption. In termination (which is what C++
supports), you assume the error is so critical that there s no way to
automatically resume execution at the point where the exception occurred. In
other words, whoever threw the exception decided there was no way to salvage
the situation, and they don t want to come back.
The alternative error-handling model is called resumption,
first introduced with the PL/I language in the 1960s. Using
resumption semantics means that the exception handler is expected to do
something to rectify the situation, and then the faulting code is automatically
retried, presuming success the second time. If you want resumption in C++, you
must explicitly transfer execution back to the code where the error occurred,
usually by repeating the function call that sent you there in the first place.
It is not unusual to place your try block inside a while loop
that keeps reentering the try block until the result is satisfactory.
Historically, programmers using operating systems that
supported resumptive exception handling eventually ended up using
termination-like code and skipping resumption. Although resumption sounds
attractive at first, it seems it isn t quite so useful in practice. One reason
may be the distance that can occur between the exception and its handler. It is
one thing to terminate to a handler that s far away, but to jump to that
handler and then back again may be too conceptually difficult for large systems
where the exception is generated from many points.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |