Throw an exception : throw « Exceptions « C++ Tutorial






#include <iostream>
using namespace std;

int main()
{

  int answer, divisor, dividend;

  try
  {
    divisor = 1;

       dividend = 0;

       if(dividend ==0)
               throw 0;

       answer = divisor/dividend;
       cout << answer;

       return 0;
  }
  catch (int i)
  {
    cout << "You cannot divide by zero";
  }
}
You cannot divide by zero"








6.2.throw
6.2.1.Throw an exception
6.2.2.Rethrowing an exception
6.2.3.Throw an exception from a function and catch it