Exception Handling in C++

Exception act as an event that takes place while executing any program, which throws the normal flow of instructions of the program.

Suppose we are writing one program where we forget to declare one variable and trying to use that variable. Our system show error that undefined symbol, this is possible by the predefined program. whenever this error occurs, it automatically displays this error on the screen. Now the user is easy to understand what is the error and also able to do the correction. This same thing is provided for C++ compiler through exception handling.

The conversion of system error message into the user-friendly message is known as exception handling.

Generally, errors are of three types compile, logical and execution time errors.

Compile time errors can be syntax, library references, Logical errors can be due to the wrong logic is written on the program. Runtime errors are also called as exception errors. Error handling can be done. Exception handling take place during runtime error.

Example:

When anyone tries to divide no of 0 i.e. y/0. It compiles successfully but generate runtime error.

Advantage of Exception Handling

It permits users to handle runtime errors and maintain the normal flow of C++ programming language application.

Types of Exception

Synchronous

These are generated due to some unusual conditions such as Array index out of bound,

overflow

Asynchronous

These exceptions are due to such events that are out of C++ program. Such as keyword

C++ exception handling mechanism is generated for handling synchronous exceptions. The main objective of the handling exception concept is to make available means for detecting and reporting an exception such that convenient action can be performed. For this, a separate error handling code is required that operate the following tasks:

  • Search and hit exceptions
  • Inform that the error has occurred (throw exception)
  • Catch exceptions in which error information can be received.
  • Handle exceptions through corrective actions

This block includes two blocks

Exception Handling in C++

Keywords used in Exception Handling

In C++ programming language, there are following three keywords used for operating Exception handling

  • Try
  • Catch
  • Throw

Throw

In C++ programming language, an exception can be thrown by the throw keyword. When some possibilities of some exceptional condition are known in the program,  the exception throw. The throw expression is used for error related communication where one parameter is accepted by throw expression and then, it is then handover to the handler.

Catch

The main usage of the catch block is to catch the program’s error and manage the exception condition. There can be multiple catch blocks in the C++ program.

Try

The main usages of the Try block to throw exceptions that can further proceed through catch blocks. There can be only one try block.

Example of exception in C++ programming language

This following two program compiles but display runtimes error

#include <iostream> 
using namespace std;
int main()
{
int p=87,q=0,r;
r=p/q;
return 0;
}

Output:

Floating-point exception (SIGFPE)


#include <iostream> 
using namespace std; 
float division(int x, int y) { 
   return (x/y); 
} 
int main () { 
   int l = 60; 
   int m = 0; 
   float n = 0; 
      n = division(l, m); 
      cout << n << endl; 
   return 0; 
}

Output:

Floating-point exception (SIGFPE)


Simple example illustrating the flow of execution of try/catch blocks in c++

#include <iostream>
using namespace std;
int main()
{
int z = -1;
cout << "Before applying try \n";
try {
              cout << "Inside the block of try \n";
              if (z < 0)
              {
                  throw z;
                  cout << "After throw (Never executed) \n";
              }
}
catch (int z ) {
              cout << "Exception in the program Caught \n";
}
cout << "After the catch block \n";
return 0;
}

Output:

Before applying try

Inside the block of try

Exception in the program Caught

After the catch block


Following are some standard exceptions found in C++ that can be used under <exception> header.

bad_typeid

bad_cast

runtime_error

logic_error

Std::exception and many more

Leave a Reply

avatar
  Subscribe  
Notify of