UserDefinedException-4
UserDefinedException-4
even. If the user enters the negative number, raise the user defined exception “Negative Number”
#include <iostream>
#include <exception>
using namespace std;
class MyException : public exception{
public:
const char * what() const throw()
{
return "Negative Number!\n";
}
};
int main()
{
try
{
int x;
cout << "Enter the number : \n";
cin >> x;
if (x< 0)
{
MyException z;
throw z;
}
else
{
if(x%2==0)
{
cout <<x<<" is Even Number" << endl;
}
else
{
cout <<x<<" is Odd Number" << endl;
}
}
}
catch(exception& e)
{
cout << e.what();
}
}