To add a bit more value to all the other answer's to this question, one should invest a few minutes in the question: What is the output of the following code?
#include <iostream>
void throw_exception() throw(const char *)
{
throw 10;
}
void my_unexpected(){
std::cout << "well - this was unexpected" << std::endl;
}
int main(int argc, char **argv){
std::set_unexpected(my_unexpected);
try{
throw_exception();
}catch(int x){
std::cout << "catch int: " << x << std::endl;
}catch(...){
std::cout << "catch ..." << std::endl;
}
}
Answer: As noted here, the program calls std::terminate()
and thus none of the exception handlers will get called.
Details: First my_unexpected()
function is called, but since it doesn't re-throw a matching exception type for the throw_exception()
function prototype, in the end, std::terminate()
is called. So the full output looks like this:
user@user:~/tmp$ g++ -o except.test except.test.cpp
user@user:~/tmp$ ./except.test
well - this was unexpected
terminate called after throwing an instance of 'int'
Aborted (core dumped)