Question: As we discussed this quarter, throwing exceptions from destructors is generally considered to be extremely problematic, so much so that we're advised never to do
As we discussed this quarter, throwing exceptions from destructors is generally considered to be extremely problematic, so much so that we're advised never to do it. Yet C++ doesn't explicitly forbid it, so it would be possible to implement a class whose destructor threw an exception.
Suppose you did write such a class:
class Throwing { public: Throwing() { } ~Throwing() { throw std::runtime_error{"doh!"}; } };
- Are there any circumstances in which this throwing destructor would be problematic? If so, demonstrate such a circumstance (e.g., by writing a C++ function that would be problematic) and briefly explain what is problematic about it. If not, demonstrate what a problematic destructor would actually look like (i.e., what else would need to be true about it, if just throwing an exception isn't enough?).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
