. , . , . . , , , . E. , ? , , terminate(). , std::set_terminate() <exception>.
, std::terminate() , , undefined.
, :
uncaught_exception() <exception> true, , . true, , , , . , , , .
Here is an example of how to use it (although this is a very bad idea): uncaught_exception()
#include <iostream>
#include <exception>
#include <stdexcept>
#include <sstream>
#include <cstdlib>
void termhandler()
{
std::cout << "Inside terminate()" << std::endl;
abort();
}
class Foo
{
public:
Foo(int val) : i(val){ std::cout << "Created Foo object " << i << std::endl; }
~Foo()
{
if(std::uncaught_exception()){
std::cout << "~Foo::Foo() object " << i << " : " << "Stack unwinding in progress. Can't throw!" << std::endl;
} else {
std::cout << "~Foo::Foo() object " << i << " : " << "Throwing test exception." << std::endl;
std::ostringstream strm;
strm << i;
std::runtime_error e("Exception from ~Foo::Foo() object " + strm.str());
throw e;
}
}
int i;
};
int main()
{
try {
std::set_terminate(termhandler);
Foo A(1);
Foo B(2);
} catch(std::exception& e){
std::cout << "Caught exception in main() : " << e.what() << std::endl;
}
}
Which gives the following conclusion:
Foo 1
object created. Foo 2 created object.
~ Foo :: Foo () object 2: test poll exception.
~ Foo :: Foo () object 1: The stack is being rotated. I can not quit!
Fixed exception in main (): Exception from object ~ Foo :: Foo () 2
Mads elvheim
source
share