C++ Program to Create Custom Exception


Exceptions are a very core concept of C++. Exceptions occur when an unwanted or impossible operation has occurred during execution. Handling these unwanted or impossible operations is known as exception handling in C++. Exception handling is mainly done using three specific keywords which are ‘try’, ‘catch’, and ‘throw’. The ‘try’ keyword is used to execute code that may encounter an exception, the ‘catch’ keyword is used to handle such exceptions, and the ‘throws’ keyword is used to create an exception. Exceptions in C++ can be divided into two types, which are STL exceptions and user-defined exceptions. In this article, we focus on creating these custom-made, user−defined exceptions. More details on exception handling can be found here.

Creating a custom exception using a singular class

At first, we see how to make a custom exception using one singular class. To do so, we have to define a class and throw exceptions of that class.

Syntax

//user-defined class
class Test{};
try{
   //throw object of that class
   throw Test();
}
catch(Test t) {
   ....
}

Example

#include <iostream>
using namespace std;

//define a class
class Test{};

int main() {
   try{
      //throw object of that class
      throw Test();
   }
   catch(Test t) {
      cout << "Caught exception 'Test'!" << endl;
   }

   return 0;
}

Output

Caught exception 'Test'!

The ‘try’ block throws the class and the ‘catch’ block catches the exception of that particular class only. If there are two user-defined exception classes, then both have to be dealt with separately.

Creating custom exceptions using multiple classes

The process is easy and as expected, if there are multiple exceptions, each has to be dealt with separately.

Syntax

//user-defined class
class Test1{};
class Test2{};
try{
   //throw object of the first class
   throw Test1();
}
catch(Test1 t){
   ....
}
try{
   //throw object of the second class
   throw Test2();
}
catch(Test2 t){
   ....
}

Example

#include <iostream>
using namespace std;

//define multiple classes
class Test1{};
class Test2{};

int main() {
   try{
      //throw objects of multiple classes
      throw Test1();
   }
   catch(Test1 t) {
      cout << "Caught exception 'Test1'!" << endl;
   }
   try{
      throw Test2();
   }
   catch(Test2 t) {
      cout << "Caught exception 'Test2'!" << endl;
   }

   return 0;
}

Output

Caught exception 'Test1'!
Caught exception 'Test2'!

We had to use two different try-catch blocks to handle the different exceptions of the two different class types. Now we see if we can create and handle exceptions using constructors.

Creating custom exceptions using constructors

We can use class constructors to create custom exceptions. We see in the below example, that the exception throwing and handling are all managed inside the class constructor itself.

Example

#include <iostream>
using namespace std;

//define a class
class Test1{
   string str;
   public:
      //try-catch in the constructor
      Test1(string str){
         try{
            if (str == "Hello"){
               throw "Exception! String cannot be 'Hello'!";
            }
            this->str = str;   
         }
         catch(const char* s) {
            cout << s << endl;
         }
      }
};

int main() {
   Test1 t("Hello");
   return 0;
}

Output

Exception! String cannot be 'Hello'!

Exception handling is one of the most important features that C++ has to offer. We can inherit the C++ exception class and use that to implement exception handling, but that is just good practice and is not necessary to create custom exceptions. The benefit of inheriting the C++ exception class is if there is a normal catch statement that catches std::exception, it can handle any user−defined exceptions without knowing the specifics. One has to take note, that the ‘throws’ statement only works inside the ‘try’ block and doesn’t work otherwise. The ‘catch’ statement can only handle an exception if one has been thrown either by the user-defined class or by some STL class.

Updated on: 13-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements