I disagree. Exceptions are for recoverable errors. If there is no sensible way to recover you could just as well abort the program right away.
|
@Peter87,
This somewhat disagrees with much of the literature.
I must grant that the subject has a range of opinions, but I focus here what I read from the literature on the subject, and of course I prefer Stroustrup where others might not.
I also want to to be clear that I'm not trying to start a debate or that I'm attempting some kind of correction to your point, but to expand on the fundamentals of the subject for readers finding this thread interesting.
In most literature there is no tendency to focus on recoverable errors for exceptions, but of all errors that are not best handled by return values logically and conveniently.
Stroustrup opens the discussion by stressing that the
fundamental idea of exceptions is the separation of detection of an error from the handling of an error. That's a far cry from focusing on recoverable errors, though it certainly doesn't discourage recovery.
Stroustrup further stresses that throwing an exception is implied when a function can't handle an error, because when that's true the function can't return normally. The reverse of that logic is that if the function can handle the error and can return normally, exceptions are not implied to solve the problem. This is what implies exceptions are for exceptional situations implied by the inability to return normally from a function.
Uncaught exceptions terminate anyway. That's nearly automatic, but it means either the programmer simply didn't catch that particular error or that it is of such severity that there's no recovery. One might hope that layers of try (from callers of code higher up) may catch exceptions not caught more locally, but the further out from the error's occurrence a catch is located the less likely it can fix the problem (usually).
The implied objective in the context of severe errors is to attempt to log and describe the unrecoverable error so it can be reported back to engineering, leading to a maintenance cycle. Feedback is an implied purpose of these types of exceptions even where recovery from the error isn't possible. There were older mechanisms supporting this notion, but C++ exceptions offer leverage.
In the mix is also the implication of saving application data upon such an unrecoverable exit. If the coding standard for the application provides "the basic guarantee" of the application's data, it may be possible to save work in "recovery storage" on the way toward termination. This is better if "the strong guarantee" can be employed, especially for those objects/storage representing user data. This is partial recovery, but not of the running application - that may be a lost cause. This is an attempt to save user's work.
For those reading not yet familiar with exceptions, there's an entire subject of writing exception safe code you'll want to search and read about, which are reflected in the patterns of the basic and strong guarantees (leaving data intact in the presence of exceptions), or the no-throw guarantee (of functions like swap). Without attention to the detail of this part of the subject, recovery from exceptions becomes less likely. It is part of exceptions one considers even when NOT writing try/catch in the code, but of everything else we write when exceptions are imposed upon that code.
That's what complicates the subject relative to @Peter87's point. Stroustrup stresses that when mixing older libraries (especially C libraries), the ability to offer such guarantees is limited or impossible. This makes it more likely exceptions can't recover, but must terminate.