What is the standard behavior in cases when function throws exception not in valid exception list ? For example when I run this code:
#include <iostream>
#include <cstdlib>
using namespace std;
void NotLegal() throw(char) {
throw 42;
}
void myunexpected () {
cout << "exception not in list\n";
cin.get();
exit(0);
}
int main()
{
set_unexpected(myunexpected);
try {
NotLegal();
}
catch(...) {
cout << "exception catched";
cin.get();
}
return 0;
}
It behaves differently on GCC and Visual Studio C++ compilers:
unexpected()
handler function is called instead of catching exception.Why is this difference ? Why MS C++ compiler doesn't calls unexpected()
callback ?
The documentation for set_unexpected
calls out this behaviour:
Remarks
...
The C++ Standard requires that
unexpected
is called when a function throws an exception that is not on its throw list. The current implementation does not support this.