The Blog

What is Exception Handling in C++

What is Exception Handling in C++

Do you know what is exception handling in C++? Have you ever thought about how to do exception handling in C++? Today we are going to experience how to do exception handling in C++.

But before we jump into the programming aspect, let’s think about the exception in general. What is an exception?

The exception is a type of dissimilar thing. This type of dissimilarity can happen in daily life also. Suppose, an individual does a particular task daily. But due to some reason, he can’t able to do that at a particular date. So, that time he made an exception to his daily routine.

Now, if he is a sincere boy what he will do next? He will rectify himself. He will try not to do such exceptions in the future. So, what is he doing by rectifying himself?

He is trying to handle the exceptions.

Now, we look at the programmer’s aspects. While writing a code there may be some exceptions that happen to the code. That exception can be from the machine side or the programmer side. But an exception can happen. So programmers will try to handle those exceptions.

That is why some methods help programmers to escape such situations. As if a code is being caught by an exception it will stop working or running. So, that will hamper the practicality of the code.

But wait! Is there any method to overcome such scenarios? Yes, there are!

What are C++ Exceptions:

C++ Exceptions are those things that can happen to any length of the program. Suppose, a programmer is written a piece of code. The programmer knows that at any point in time the program gets stopped. But why? At any point, the program can generate a particular random number for that code. An inappropriate random number can stop the execution.

Let’s take an example:

Suppose, the main goal of a program is to divide any particular number with any random number from 0 to 10. Now, at any point in time number is generated as zero. So, dividing by zero will always prompt an output. This is called an exception. This exception can happen at any time.

So, the programmer knows such a scenario can happen. So, the programmer will use a particular method. That will help to overcome such errors. This method is called exception handling in C++.

 

Types of C++ Exceptions? Know More

There are mainly two types of exceptions present in the world of programming. Not only C++ but also all types of Object-Oriented Programming (OOP) Languages have such exceptions. They are:

  • Synchronous Exception or Programmer Made Exception:

These exceptions are made by a programmer. Or we can state it like, these exceptions can be thought of by a programmer while writing a code. Like, as division by zero. So, these types of exceptions can be easily removed. The maximum type of exceptions is from this category.

  • Asynchronous Exception or Machine-Made Exception:

These exceptions can’t be controlled or removed by programmers. These types of exceptions are closely associated with the machine itself. Due to memory failure or disk interruption or due to keyboard interruption these types of exceptions can be found. Though these types of exceptions are very rare to see.

What is Exception Handling in C? Read Below

There is only one method present for exception handling in C++. The method consists of three sub-methods.

But before we move forward, let’s know those sub-methods:

  • C++ Try Sub Method:

In this block, we have to write some piece of code. And the part of code which can prompt an exception will be thrown.

General Syntax: try{//statement}

  • C++ Throw Sub Method:

This is a single-line statement. We have to write the piece of code which can create the exception. The throwing method will move the section of code to the catch method.

General Syntax: throw statement;

  • C++ Catch Sub Method:

The catch method will receive the piece of code as an argument & provide a default output. Hence the program will not get terminated abnormally.

General Syntax: catch (argument) {//statement}

We can divide the Try Catch method into two further methods also. They are:

  1. Try Catch C++ : Normal Method
  2. Try Catch C++: Catch All Method

exception handling in c++

Normal Method – C++ Try Catch Example:

In this method, we normally use the Try Catch block. The same syntax we use here. That is why it is often called as Normal Method.

Here, we have to take two user input values. In between them, the second one can be sometimes zero. As we are performing the division, division by zero can’t happen. That is why we here used normal Try Catch blocks.

If the second number is zero, then it will throw the exception to the Catch block. The Catch block will receive the second number as an argument & print a default statement.

If the second number is not zero, then it will simply do the division operation & provide the output.

Hence, this is the only simple method that helps us to do exception handling in C++.

Example:

#include <iostream>

using namespace std;

int main()

{

    int a,b; // Declaring The Variables

    // Taking Input From User For The First Number

    cout << "Enter First Number: ";

    cin >> a;

    // Taking Input From User For The Second Number

    cout << "Before Second Number: ";

    cin >> b;

    // Implementing try Block

    try

{

            // Checking The Second Number Zero or Not

                      if (b == 0)

                      {

                          // If The Second Number Is Zero Throw Statement Will Work

                                     throw b;

                      }

                      // If The Second Number Is Not Zero Then The Below Statement Will Work

                      else

                      {

                          cout << "Result Is: " << a/b; // Result Will Be Printed

                      }

        }

    // Catch Block Accepts The Second Value As Argument

    catch (int b)

    {

                  cout << "Division By Zero Not Allowed"; // Printting The Default Statement

    }

    return 0;

}

Let’s look at the output of the above code. Hence, we come to know about exception handling in C++

Output:

Without Divided By Zero

Normal method C++

With Divided By Zero

Normal Method C++

Catch All Method – C++ Try Catch Example:

In this case, all types of exceptions can be able to accept the block. Here, we have declared one integer variable. We are throwing that as an exception. There are two types of catch blocks. One is for catching character-related exceptions & another for all types of exceptions.

The catch block for catching the character-related exception can’t able to receive the integer exception. So, in this case, the second catch block will receive that.

It doesn’t have any argument specification. That is why it can able to take any exceptions.

General Syntax: catch(…){//statement}

Example:

#include <iostream>

using namespace std;

int main(){

              int x=10; // Declaring The Variable

              // Implementing try Block

              try {

                  throw x; // Throwing The Exception

              }

              // Catch Block To Accept The Exceptions Related to Characters

              catch (char x) {

                             cout << "Caught In Normal Method";

              }

              // Catch Block To Accept All Type Of Exceptions

              catch (...) {

                             cout << "Caught In Catch All Method";

              }

              return 0;

}

Let’s look at the output of the above code. Hence, we come to know about exception handling in C++

Output:

Catch all method C++

Conclusion:

As we saw exception handling in C++ is very important.

It will help in the future when you interact with various kinds of large problems.

Exception handling helps us to assume the situation before even executing the code. It helps a lot to understand the situation.

Also, if you guys are looking to get help with C Programming Assignments then you can use our C ++ Programming Homework Help Services.

If you are looking for Final Year C Programming Project Ideas then you can contact us as well.

 

Sanjana Grover
Sanjana Grover

Leave a Comment