Answer the question
In order to leave comments, you need to log in
How to catch multiple exceptions in destructors when inheriting?
#include <iostream>
#include <stdexcept>
class A
{
public:
A()
{
std::cout << "A" << std::endl;
fake();
}
virtual ~A()
{
throw std::runtime_error("Test ex in ~A()");
std::cout << "~A" << std::endl;
}
void fake()
{
d();
}
virtual void d()
{
std::cout << "A::d()" << std::endl;
}
};
class B : public A
{
public:
B()
{
std::cout << "B" << std::endl;
fake();
}
~B()
{
std::cout << "~B" << std::endl;
}
virtual void d()
{
std::cout << "B::d()" << std::endl;
}
};
class C : public B
{
public:
C()
{
std::cout << "C" << std::endl;
fake();
}
~C()
{
throw std::runtime_error("Test ex in ~C()");
std::cout << "~C" << std::endl;
}
virtual void d()
{
std::cout << "C::d()" << std::endl;
}
};
int main( int argc, char *argv[] )
{
try {
{
C object;
}
}
catch (const std::exception& e) {
std::cout << "catch: " << e.what() << std::endl;
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
And because it is not necessary to release exceptions from the destructor - it is necessary to catch all of them in the same place.
Specifically in your case - if an exception is thrown from the destructor while another exception is being handled, the application will terminate.
stackoverflow.com/questions/130117/throwing-except...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question