// File: eh6.cpp
#include <iostream>
using namespace std;
class X
{
int a;
double b;
public:
X(int i, double j) : a(i), b(j) { }
friend ostream& operator<<(ostream& o,const X& x)
{
return o << x.a << ' ' << x.b;
}
};
void funk(int i)
{
try
{
switch (i)
{
case 1: throw("Have a nice day");
case 2: throw(5);
case 3: throw(3.14);
case 4: throw(5L);
case 5: throw(&i);
case 6: throw(X(5,3.14));
}
}
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(float it)
{
cout << "You threw me a float: " << it << endl;
}
catch(double it)
{
cout << "You threw me a double: " << it << endl;
}
catch(long it)
{
cout << "You threw me long: " << it << endl;
}
catch(int* it)
{
cout << "You threw me an int address: " << it << endl;
}
catch(X& it)
{
cout << "You threw me an X: " << it << endl;
}
}
int main()
{
funk(1);
funk(2);
funk(3);
funk(4);
funk(5);
funk(6);
cout << "End of program\n";
return 0;
}
***** Output *****
You threw me a const char*: Have a nice day You threw me an int: 5 You threw me a double: 3.14 You threw me long: 5 You threw me an int address: 0012FE98 You threw me an X: 5 3.14 End of program
Which catch did not get used?
What if you throw a type that you haven't written a catch for?