C++ Exception Handling - try throw catch

Advertisements

C++ Exception Handling - try catch

An exception is a situation, which occured by the runtime error. In other words, an exception is a runtime error. An exception may result in loss of data or an abnormal execution of program.

Exception handling is a mechanism that allows you to take appropriate action to avoid runtime errors.

C++ provides three keywords to support exception handling.

  • Try : The try block contain statements which may generate exceptions.
  • Throw : When an exception occur in try block, it is thrown to the catch block using throw keyword.
  • Catch :The catch block defines the action to be taken, when an exception occur.

The general form of try-catch block in c++.


		

Example of simple try-throw-catch


       #include<iostream.h>
       #include<conio.h>
       void main()
       {
              int n1,n2,result;

              cout<<"\nEnter 1st number : ";
              cin>>n1;

              cout<<"\nEnter 2nd number : ";
              cin>>n2;

              try
              {
                  if(n2==0)
                      throw n2;         //Statement 1
                  else
                  {
                      result = n1 / n2;
                      cout<<"\nThe result is : "<<result;
                  }
              }
              catch(int x)
              {
                  cout<<"\nCan't divide by : "<<x;
              }

              cout<<"\nEnd of program.";

       }

   Output :

              Enter 1st number : 45
              Enter 2nd number : 0
              Can't divide by : 0
              End of program

				

The catch block contain the code to handle exception. The catch block is similar to function definition.


              catch(data-type arg)
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
                     - - - - - - - - - -
              };

				

Data-type specifies the type of exception that catch block will handle, Catch block will recieve value, send by throw keyword in try block.

Multiple Catch Statements

A single try statement can have multiple catch statements. Execution of particular catch block depends on the type of exception thrown by the throw keyword. If throw keyword send exception of integer type, catch block with integer parameter will get execute.

Example of multiple catch blocks


       #include<iostream.h>
       #include<conio.h>
       void main()
       {
            int a=2;

              try
              {

                  if(a==1)
                      throw a;                  //throwing integer exception

                  else if(a==2)
                      throw 'A';                //throwing character exception

                  else if(a==3)
                      throw 4.5;                //throwing float exception

              }
              catch(int a)
              {
                  cout<<"\nInteger exception caught.";
              }
              catch(char ch)
              {
                  cout<<"\nCharacter exception caught.";
              }
              catch(double d)
              {
                  cout<<"\nDouble exception caught.";
              }

              cout<<"\nEnd of program.";

       }

   Output :

              Character exception caught.
              End of program.

				

Catch All Exceptions

The above example will caught only three types of exceptions that are integer, character and double. If an exception occur of long type, no catch block will get execute and abnormal program termination will occur. To avoid this, We can use the catch statement with three dots as parameter (...) so that it can handle all types of exceptions.

Example to catch all exceptions


       #include<iostream.h>
       #include<conio.h>
       void main()
       {
            int a=1;

              try
              {

                  if(a==1)
                      throw a;                  //throwing integer exception

                  else if(a==2)
                      throw 'A';                //throwing character exception

                  else if(a==3)
                      throw 4.5;                //throwing float exception

              }
              catch(...)
              {
                  cout<<"\nException occur.";
              }

              cout<<"\nEnd of program.";

       }

   Output :

              Exception occur.
              End of program.

				

Rethrowing Exceptions

Rethrowing exception is possible, where we have an inner and outer try-catch statements (Nested try-catch). An exception to be thrown from inner catch block to outer catch block is called rethrowing exception.

Syntax of rethrowing exceptions


		

Example of rethrowing exceptions


       #include<iostream.h>
       #include<conio.h>
       void main()
       {
            int a=1;

              try
              {
                      try
                      {
                              throw a;
                      }
                      catch(int x)
                      {
                          cout<<"\nException in inner try-catch block.";

                          throw x;
                      }

              }
              catch(int n)
              {
                  cout<<"\nException in outer try-catch block.";
              }

              cout<<"\nEnd of program.";

       }

   Output :

              Exception in inner try-catch block.
              Exception in outer try-catch block.
              End of program.

				

Restricting Exceptions

We can restrict the type of exception to be thrown, from a function to its calling statement, by adding throw keyword to a function definition.

Example of restricting exceptions


       #include<iostream.h>
       #include<conio.h>

       void Demo() throw(int ,double)
       {
            int a=2;

                  if(a==1)
                      throw a;                  //throwing integer exception

                  else if(a==2)
                      throw 'A';                //throwing character exception

                  else if(a==3)
                      throw 4.5;                //throwing float exception

       }

       void main()
       {

              try
              {
                  Demo();
              }
              catch(int n)
              {
                  cout<<"\nException caught.";
              }

              cout<<"\nEnd of program.";

       }

				

The above program will abort because we have restricted the Demo() function to throw only integer and double type exceptions and Demo() is throwing character type exception.

Advertisement