User-defined Custom Exception with class in C++ - GeeksforGeeks

16 Apr, 2021
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  
CPP14
#include <iostream>using namespace std; class demo {}; int main(){    try {        throw demo();    }     catch (demo d) {        cout << "Caught exception of demo class \n";    }}
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  
CPP14
#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";        }    }}
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:  
CPP14
#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";        }    }}
Output:  
Caught exception of demo1 class
Caught exception of demo1 class
Explanation: The program is similar to previous 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:  
CPP14
#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);}
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 . 
simmytarika5
C++-Exception Handling
C++ Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
cin in C++
Shallow Copy and Deep Copy in C++
Check if given number is perfect square
C++ Program to check if a given String is Palindrome or not
Passing a function as a parameter in C++
Program to implement Singly Linked List in C++ using class
C Program to Swap two Numbers
CSV file management using C++
cout in C++
Pi(π) in C++ with Examples