Exception Handling - Example 9 - set_terminate()
#include <iostream>
#include <exception> // for set_terminate()
#include <string>
using namespace std;
void uncaught()
{
cerr << "I wasn't able to catch an exception\n";
}
void funk(int i)
{
try
{
switch (i)
{
case 1: throw(string("have a nice day"));
case 2: throw(5);
case 3: throw(3.14);
}
}
catch(const string& it)
{
cout << "You threw me a string: " << it << endl;
}
catch(double it)
{
cout << "You threw me a double: " << it << endl;
}
}
int main()
{
set_terminate(uncaught);
funk(1);
funk(2);
funk(3);
cout << "End of program\n";
return 0;
}
***** Program Output ***** (Microsoft Visual C++, same for gnu running on Windows)
You threw me a string: have a nice day
I wasn't able to catch an exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
***** Program Output ***** (gnu: g++ 4.3.2 on linux)
You threw me a string: have a nice day
I wasn't able to catch an exception
Abort