CPP Programming
About Lesson

Exception handling in C++ is done using three keywords called try, throw and catch.  The suspected code that would cause error is to be kept inside try block.  The throw keyword is used to throw corresponding error code from try block.  Based on the error code coming out from try block the corresponding catch block will be executed to handle the exception raised.  Each try block must be followed by atleast one catch block.  One try block can follow many catch blocks but only one catch block will be executed.

 

 

#include<iostream>

using namespace std;

 

int main()

{

    int a,b;

 

    cin>>a>>b;

 

    try

    {

        if(b==0) throw 0;

        cout<<“\na/b= “<<a/b;

    }

    catch(int x)

    {

        cout<<“\nDivisible zero is not possible”;

    }

    cout<<“\nRest of the program”;

}

 

The code following the throw keyword will not executed.  Hence the developer has to take care to keep only the risky code inside the try block.

 

#include<iostream>

using namespace std;

 

 

int main()

{

    try

    {

        cout<<“before try\n”;

        throw 1;

        cout<<“after try\n”;

    }

    catch(int x)

    {

        cout<<x<<” is thrown\n”;

    }

    cout<<“\nThank you”;

}

 

The following are different cases related to exception handling:

  1. No exception raised inside try block
  2. Exception raised and handled by corresponding catch block
  3. Exception raised and not handled by catch block.

 

#include<iostream>

using namespace std;

 

int main()

{

    try

    {

        cout<<“before try\n”;

        throw 1;

        cout<<“after try\n”;

    }

    catch(char x)

    {

        cout<<x<<” is thrown\n”;

    }

    cout<<“\nThank you”;

}

 

The above program can be re-written like below, using catch(…) to handle any kind of exception.

 

#include<iostream>

using namespace std;

 

 

int main()

{

    try

    {

        cout<<“before try\n”;

        throw 1;

        cout<<“after try\n”;

    }

    catch(char x)

    {

        cout<<x<<” is thrown\n”;

    }

    catch(…)

    {

        cout<<“exception handled”;

    }

    cout<<“\nThank you”;

}

 

When an exception occurred after an object is created in the memory, before going out from the try block destructor will be automatically get executed.

 

#include<iostream>

using namespace std;

 

class test

{

public:

    test()

    {

        cout<<“\nConstructor is called”;

    }

    ~test()

    {

        cout<<“\nDestructor is called”;

    }

};

 

int main()

{

    try

    {

        test t;

        cout<<“\ncode before throw”;

        throw 1;

        cout<<“\ncode after throw”;

    }

    catch(int x)

    {

        cout<<“\nException handled”;

    }

    cout<<“\nEnd of the program”;

}

 

Nested Try

One try block can contain another try block inside its block.

 

#include<iostream>

using namespace std;

 

int main()

{

    try

    {

        cout<<“\nouter try code before throw”;

        try

        {

            cout<<“\nInner Try code before throw”;

            throw 2;

            cout<<“\nInner Try code after throw”;

        }

        catch(int x)

        {

            cout<<“\nException of Inner Try is handled”;

        }

        cout<<“\nouter code after throw”;

    }

    catch(int x)

    {

        cout<<“\nException of Outer Try is handled”;

    }

    cout<<“\nEnd of the program”;

}

 

You cannot copy content of this page