C++ program to illustrate nested Try/Catch construct

#include <iostream>
using namespace std;
int main()
{
  int d;
  char msg[]="Divide by zero error.";
  try
  {
    cout<<"Enter divisor: ";
    cin>>d;
    if(d==0)
      throw d;
    else
      cout<<"Quotient="<<1000/d;
  }
  catch(int d)
  {
    cout<<msg;
    cout<<endl<<"Enter divisor again: ";
    cin>>d;
    try
    {
      if(d==0)
        throw d;
      else
        cout<<"Quotient="<<1000/d;
    }
    catch(int x)
    {
      cout<<"OOPs !!! You entered "<<x<<" again..";
    }
  }
  return 0;
}

No comments:

Post a Comment