exception handling
#include<iostream>
#include<string>
using namespace std;
class Exception
{
public:
Exception(int _x,int _y):x(_x),y(_y) { }
double divide();
string getMsg();
private:
int x;
int y;
};
double Exception::divide()
{
if(y==0)
{
throw *this; // we return the adress
}
return ((double)x/y);
}
string Exception::getMsg()
{
return "can not divide by zero"; // error message
}
int main()
{
Exception p(5,0);
Exception k(3,2);
double result;
try
{
result=p.divide();
}
catch (Exception &err) // we get the address here
{
cout<<err.getMsg()<<endl; //error output
}
try
{
result=k.divide();
}
catch (Exception &err) // we get the address here
{
cout<<err.getMsg()<<endl; //error output
}
cout<<"result: "<<result<<endl;
return 0;
}