Exception Handling - Example 10 - Exception specifications
// File: eh10.cpp - Exception specifications
#include <iostream>
#include <exception>
using namespace std;
// function prototype with exception specifications
void funk(int i) throw(const char*, int, double);
int main()
{
funk(1);
funk(2);
funk(3);
funk(4); // cause execution of unexpected()
cout << "End of program\n";
return 0;
}
// Note: you must repeat specification list
void funk(int i) throw(const char*, int, double)
{
try
{
switch (i)
{
case 1: throw("Have a nice day");
case 2: throw(5);
case 3: throw(3.14);
case 4: throw(5L);
}
}
catch(const char* it)
{
cout << "You threw me a const char*: " << it << endl;
}
catch(int it)
{
cout << "You threw me an int: " << it << endl;
}
catch(double it)
{
cout << "You threw me a double: " << it << endl;
}
}
***** Program Output ***** (Microsoft Visual C++ / gnu 3.4.2 running on Windows)
You threw me a const char*: Have a nice day
You threw me an int: 5
You threw me a double: 3.14
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 const char*: Have a nice day
You threw me an int: 5
You threw me a double: 3.14
terminate called after throwing an instance of 'long'
Abort