C++ program to illustrate the use of multiple Catch blocks in Exception Handling

#include <iostream>
using namespace std;
int main()
{
  char msg[]="Divide by Zero error";
  int divisor,i;
  int num[]={45,67,100,34,121};
//5 data
  try
  {
    cout<<"Enter divisor: ";
    cin>>divisor;
    for(i=0;i<=5;i++)
    {
      if(divisor==0)
        throw msg;
      if(i==5)
        throw i;
      cout<<endl<<num[i]/divisor;
    }
  }
  catch(char warning[])
  {
    cout<<endl<<warning;
  }
  catch(int n)
  {
    cout<<endl<<"Index "<<n<<" is out of range.";
  }
  return 0;
}

No comments:

Post a Comment