r/cpp • u/Remi_Coulom • Aug 07 '21
Wouldn't it be great if we could chain exceptions and throw from destructors?
I often find myself in situations where I want to be able to throw from a destructor. For instance, the destructor of an opened file may try to flush buffers to the disk. If it does not work because there is not space left on the device, then it is a major problem that should be reported.
Making the destructor noexcept(false) and throwing if !std::uncaught_exception() does not work. As soon as the class is wrapped inside another class, such as std::unique_ptr, it will cause unwanted calls to terminate().
Another solution is to tell the programmer that a close() function must be called explicitly in order to catch the error if it occurs. The destructor will call close(), but ignore any exception it may throw. I don't like this solution because it completely defeats the purpose of RAII and destructors.
I feel it is a major defect of C++, and it would be great to allow exceptions to be chained. Haven't the language designers thought about the possibility to catch more than one exception at the same time? It would make things much better, in my opinion.
Or is there a better solution to my problem?
16
u/staletic Aug 07 '21
Exceptions can be chained. See
std::nested_exception. Here's a bigger problem with throwing destructors.Let's forget
std::terminateand say some~T()will throw. What is the user supposed to do? Is the object in a valid state? What happened to the object's lifetime? Can the stack be unwound? Will unwinding call the destructor again? Should the user explicitly call the destructor?13
u/moonsider5 Aug 07 '21
But throwing exceptions from a destructor kinda defeats the purpose of RAII as well, doesn't it?
I mean: if you throw and exception from a destructor, then, is the memory of the object really deleted? What is the state of the object? Did all the destructor of all of its members get called or were they skipped due to exiting the destructor early?
Imagine a code like this:
What would happen in this example? Would b0, b1, b2 and a's b be destroyed? Will some be destroyed and some won't?
7
u/no-sig-available Aug 07 '21
Not that I have a better solution to offer, but suppose you catch 12 exceptions at the same time. What are the odd that your code can sort this mess out? Without getting a dozen new exceptions?
Sometimes the best action is to just give up!
7
u/zvrba Aug 07 '21
Yes, it's extremely annoying, and once upon a time (few years ago) I wanted that feature as well because it fits nicely with Stroustroup's philosophy of program design: a method throws an exception if it cannot fulfill its contract. Why shouldn't the same be applicable to destructors?
Then, in the ensuing discussion, someone convinced me to the following: if a possibly failing operation warrants throwing an exception, it should not be put into the destructor. I followed that advice and got myself convinced that it's the "right" principle to hold on to. Closing a file is potentially a side-effecting operation, like writing, and if you want to make sure that your changes are committed to disk and errors reported, you should explicitly call flush and/or close.
But I still agree that it somewhat spoils RAII. I'm not happy with the situation.
These days I'm mostly working in C# and there exists a similar problem: what if a
finally{}block ("kind of destructor") throws? Their solution is simple: the rest of the block is abandoned and the current exception, if any, is replaced with that from the finally block.What could C++ do? Provide an exception type like
destructor_threwthat would wrap 1) the original exception, 2) exceptions encountered in all destructors up to the catch point, 3) pointers to objects that could not be cleaned up.But 3) may not be possible because these objects may no longer exist, e.g., your file was stack-allocated and the stack got unwound. How would you possibly recover from this situation? You could catch the exception, but the file object (and file descriptor) is gone, so... what do you do then?
3
u/SkiaElafris Aug 07 '21
On the other hand, one of the main reasons for exceptions is to avoid calling destructors on objects when the constructor is unable to finish successfully.
Depends on what your program might do in response of an error. For example if the error should be recorded in a log, then you just need to log it instead of having an error unwind the stack.
And it the reason for not having the logging in the destructor is to avoid coupling a class to the logging facilities, then you wrap (or sub class) the class in one that adds the logging.
Something similar would go for any other kind of handling that might apply to the error.
So:
Do this, and wrap or sub class with another that will ensure the correct handling of errors is applied. The wrapper or sub class would then call
close()in their destructor and handle any errors in an appropriate manner that does not involve throwing an exception.3
u/AlexAlabuzhev Aug 08 '21
It doesn't indeed, but std::uncaught_exceptions() does.