Exceptions are runtime anomalies that a program encounters during execution. It is a situation where a program has an unusual condition and the section of code containing it can’t handle the problem. Exception includes condition such as division by zero, accessing an array outside its bound, running out of memory, etc.
In order to handle these exceptions, exception handling mechanism is used which identifies and deal with such condition. Exception handling mechanism consists of following parts:
- Find the problem (Hit the exception)
- Inform about its occurrence (Throw the exception)
- Receive error information (Catch the exception)
- Take proper action (Handle the exception)
C++ consists of 3 keywords for handling the exception. They are
- try: Try block consists of the code that may generate exception. Exception are thrown from inside the try block.
- throw: Throw keyword is used to throw an exception encountered inside try block. After the exception is thrown, the control is transferred to catch block.
- catch: Catch block catches the exception thrown by throw statement from try block. Then, exception are handled inside catch block.
Syntax
try { statements; ... ... ... throw exception; } catch (type argument) { statements; ... ... ... }
Multiple catch exception
Multiple catch exception statements are used when a user wants to handle different exceptions differently. For this, a user must include catch statements with different declaration.
Syntax
try { body of try block } catch (type1 argument1) { statements; ... ... ... } catch (type2 argument2) { statements; ... ... ... } ... ... ... ... ... ... catch (typeN argumentN) { statements; ... ... ... }
Catch all exceptions
Sometimes, it may not be possible to design a separate catch block for each kind of exception. In such cases, we can use a single catch statement that catches all kinds of exceptions.
Syntax
catch (...) { statements; ... ... ... }
Note: A better way is to use catch(…) as a default statement along with other catch statement so that it can catch all those exception which are not handled by other catch statements.
Example of exception handling
C++ program to divide two numbers using try catch block.
#include <iostream> #include <conio.h> using namespace std; int main() { int a,b; cout << "Enter 2 numbers: "; cin >> a >> b; try { if (b != 0) { float div = (float)a/b; if (div < 0) throw 'e'; cout << "a/b = " << div; } else throw b; } catch (int e) { cout << "Exception: Division by zero"; } catch (char st) { cout << "Exception: Division is less than 1"; } catch(...) { cout << "Exception: Unknown"; } getch(); return 0; }
This program demonstrate how exception are handled in C++. This program performs division operation. Two numbers are entered by user for division operation. If the dividend is zero, then division by zero will cause exception which is thrown into catch block. If the answer is less than 0, then exception “Division is less than 1” is thrown. All other exceptions are handled by the last catch block throwing “Unknown” exception.
Output
Enter 2 numbers: 8 5 a/b = 1.6 Enter 2 numbers: 9 0 Exception: Division by zero Enter 2 numbers: -1 10 Exception: Division is less than 1