Exception Handling - Example 11 - set_unexpected()

// File: eh11.cpp - set_unexpected()

#include <iostream>
#include <exception>
using namespace std;

// function prototype with exception specifications
void funk(int i) throw(const char*, int, double);
void Unexpected();

int main()
{
	set_unexpected(Unexpected);
	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;
	}
}

void Unexpected()
{
	cerr << "I didn't expected this to happen!\n";
}

***** 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
I didn't expected this to happen!

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
I didn't expected this to happen!
terminate called after throwing an instance of 'long'
Abort