User-defined Custom Exception with class in C++

We can use Exception handling with class too. Even we can throw an exception of user defined class types. For throwing an exception of say demo class type within try block we may write

throw demo();

Example 1: Program to implement exception handling with single class

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class demo {
};
  
int main()
{
    try {
        throw demo();
    }
  
    catch (demo d) {
        cout << "Caught exception of demo class \n";
    }
}

chevron_right


Output:

Caught exception of demo class

Explanation: In the program we have declared an empty class.In the try block we are throwing an object of demo class type. The try block catches the object and displays.

Example 2: Program to implement exception handling with two class

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class demo1 {
};
  
class demo2 {
};
  
int main()
{
    for (int i = 1; i <= 2; i++) {
        try {
            if (i == 1)
                throw demo1();
  
            else if (i == 2)
                throw demo2();
        }
  
        catch (demo1 d1) {
            cout << "Caught exception of demo1 class \n";
        }
  
        catch (demo2 d2) {
            cout << "Caught exception of demo2 class \n";
        }
    }
}

chevron_right


Output:

Caught exception of demo1 class
Caught exception of demo2 class

Exception handling with inheritance:

Exception handling can also be implemented with the help of inheritance. In case of inheritance object thrown by derived class is caught by the first catch block.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class demo1 {
};
  
class demo2 : public demo1 {
};
  
int main()
{
    for (int i = 1; i <= 2; i++) {
        try {
            if (i == 1)
                throw demo1();
  
            else if (i == 2)
                throw demo2();
        }
  
        catch (demo1 d1) {
            cout << "Caught exception of demo1 class \n";
        }
        catch (demo2 d2) {
            cout << "Caught exception of demo2 class \n";
        }
    }
}

chevron_right


Output:

Caught exception of demo1 class
Caught exception of demo1 class

Explanation: The program is similar to previos one but here we have made demo2 as derived class for demo1.Note the catch block for demo1 is written first.As demo1 is base class for demo2 an object thrown of demo2 class will be handled by first catch block.That is why output as shown.

Exception handling with constructor:

Exception handling can also be implemented by using constructor. Though we cannot return any value from the constructor but with the help of try and catch block we can.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class demo {
    int num;
  
public:
    demo(int x)
    {
        try {
  
            if (x == 0)
                // catch block would be called
                throw "Zero not allowed ";
  
            num = x;
            show();
        }
  
        catch (const char* exp) {
            cout << "Exception caught \n ";
            cout << exp << endl;
        }
    }
  
    void show()
    {
        cout << "Num = " << num << endl;
    }
};
  
int main()
{
  
    // constructor will be called
    demo(0);
    cout << "Again creating object \n";
    demo(1);
}

chevron_right


Output:

Exception caught
 Zero not allowed
Again creating object
Num = 1

Explanation: when x = 0 exception is thrown and catch block is called. And when x = 1 no exception is created .



My Personal Notes arrow_drop_up

A fallen star which will rise again

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.