Write a program that uses inheritance to create various derived classes of runtime_error . Then show that a catch
handler specifying the base class can catch derived-class exceptions?
am i doing it rite this is my code :
#include <stdexcept>
#include <exception>
using std::runtime_error;
class DivideByZeroException : public runtime_error
{
public :
DivideByZeroException()
:runtime_error("attempt to devide by zero" ) {}
};
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "DivideByZeroException.h"
double quotient ( int numerator, int denominator )
{
if ( denominator == 0 )
throw DivideByZeroException();
return static_cast<double>( numerator ) / denominator;
}
int main ()
{
int number1;
int number2;
double result;
cout << "Enter two integers ( end-of-file to end ) : ";
while ( cin >> number1 >> number2 )
{
try
{
result = quotient( number1, number2 );
cout << "The quotient is: " << result << endl;
}
catch ( DivideByZeroException &error )
{
cout << "Exception occured: " << error.what() << endl;
}
cout << "\nEnter two integers ( end-of-file to end ) : ";
}
cout << endl;
return 0;
}