Exception Handling - Example 13 - Throwing more than one value
// File: eh13.cpp - Throwing more than one value
#include <iostream>
#include <cstring>
using namespace std;
class error_stuff
{
int x;
float y;
char z;
public:
error_stuff(int xx, float yy, char zz)
: x(xx), y(yy), z(zz)
{ }
int get_x (void){return x; }
float get_y (void){return y; }
char get_z (void){return z; }
};
int main()
{
int i;
float f;
char c;
char x[20];
strcpy(x,"hey");
try
{
cout << "Enter an int, a positive float, and a char => ";
cin >> i >> f >> c;
if (f < 0) throw error_stuff(i,f,c);
else cout << "Thanks\n";
}
catch(error_stuff& e)
{
cerr << "Hey, you entered " << e.get_x() << ' ' <<
e.get_y() << ' ' << e.get_z() << endl;
}
cout << "*** End of Program ***\n";
return 0;
}
***** Sample Execution *****
Enter an int, a positive float, and a char => 3 4.4 c
Thanks
*** End of Program ***
Enter an int, a positive float, and a char => 3 -3.3 z
Hey, you entered 3 -3.3 z