Throw your own exception class based on runtime_error : Custom Exception « Exceptions « C++ Tutorial






#include <iostream>
#include <stdexcept>
using std::cin;
using std::cout;
using std::endl;
using std::runtime_error;

class DivideByZeroException : public runtime_error
{
public:
   DivideByZeroException::DivideByZeroException(): runtime_error( "attempted to
 divide by zero" ) {}
};

double quotient( int numerator, int denominator )
{
   throw DivideByZeroException(); // terminate function

   return 0;
}

int main()
{
    try
    {
       double result = quotient( 1, 1 );
       cout << "The quotient is: " << result << endl;
    }
    catch ( DivideByZeroException &divideByZeroException )
    {
       cout << "Exception occurred: " << divideByZeroException.what() << endl;

    }

    return 0;
}
Exception occurred: attempted to divide by zero








6.5.Custom Exception
6.5.1.Throw your own exception class based on runtime_error
6.5.2.Custom exception class
6.5.3.Throw a custom exception object
6.5.4.Use custom exception in your own Array class