Custom User Defined Exceptions in C++

Exception handling is a very important concept in OOPS. What is this exception handling and how to handle the exception in C++?

In this tutorial, we will get to know about the brief explanation of the exception handling concept and later on, we will concentrate on custom user defined exception handling concept.

Let’s take an example, we are going to divide 12 by 0. We have written all the program and executed the program. As soon as it will compile we will face an error which is nothing but a run time error. There will be an error statement like can’t be divided by zero. That is called an exception occurs.

In this tutorial, we will especially concentrate on user-defined exceptions in C++.

User Defined Exception

We have to remember certain rules in order to write user-defined exception :

  1.          Always include exception header using pre-processor directive at the very first step.
  2.          The function which will return an exception string should have a return type of char followed by *,
char* what()
{
 // codes here
}

char is as return type because we will return a string.

3.     Should have a try and catch block.

Now let’s have an example first and then we will discuss in details what are the things going on inside the code.

#include<iostream>
#include<exception>

class Divide_By_Zero:public exception
 {
  public:
   const char *what() const throw
   {
    return "Can't perform Divide by Zero operation";
   }
 };

int main()
{
  Divide_By_Zero d;
  exception ex;
  int x,y;
  cout<<"Take the two numbers: "<<endl;
  cin>>x>>y;
  try
  {
   if(x<=0 || y<=0)
     throw d;
   else
    cout<<"Here is the answer of divide operation "<<(x/y);
  }
  catch(exception& ex)
  {
   cout<<ex.what();
  }
  return 0;
}

Now we will explore the above program in detail.

1. In order to create a custom program, we have included a header file initially along with iostream.

2. It’s time to create exception class,

class Divide_By_Zero:public exception
{
   public:
   const char* what() const throw()
   {
     return "Can't perform Divide by Zero operation";
   }
};
  •  Here we have created a class, named as Divide_By_Zero which will inherit base class exception.
  • Inside the exception class, Divide_By_Zero, we will have the functions. Here we have the function char* what() which is of const type. The reason behind this const keyword is we don’t want to change the message what we are returning or doesn’t want to deform the message.
  • And on the very next line, we are returning the error message

3.  On the next line, we have created two objects one of user defined exception class and one for the exception class.

4. Then inside try block we have operated the divide functions and if there error occurs in the logic or mathematical operation, we will throw the exception class object.

5. Inside the catch block, we have included the exception object with its exact address pointer. There we are showing the result by calling what function through the exception object.

catch(exception& ex)
{
  cout<<ex.what();
}

This is how we can create a user defined exception class in C++.

 

2 responses to “Custom User Defined Exceptions in C++”

  1. Saptarishi Karmakar says:

    In the exception catch statement the type of object ex must be Divide_By_Zero and not exception.
    Disappointed

  2. Arijit Nayak says:

    It’s not mandatory to throw with the same class object type. Exception keyword with the pointer will take the current executable exception class and it’s exception. Also you look to the class there is one type of return object assigned to the pointer. And also look above in the main class about the creation of the “ex” object and explore more how the main class will be executed.

Leave a Reply

Your email address will not be published. Required fields are marked *