Exception Handling

Exceptions are the run time anomalies or unusual conditions that a program may encounter while executing. Examples- division by zero, access to an array outside of its bounds, running out of memory space. Because exceptions are outside the normal operation of a program, default action is to write out an error message and terminate the offending process.

Type of Exceptions

  • Synchronous – Errors such as “out of range” and “over-flow” are synchronous.
  • Asynchronous – Errors that are caused by events beyond the control of the program (keyboard interrupts) are called asynchronous exceptions


Try block- A block of statements which may generate exceptions. To throw an exception is to signal that exception has occurred. To catch an exception is to transfer control to an exception handling routine (catch block), altering the normal execution flow of the program. When try block throws the exception the program leaves the try block and enters the catch block. Exceptions are the objects used to transmit information about the problem. If the type of object thrown matches the arg type of catch statement then catch block is executed , else abort() function is invoked by default. When no exception is thrown the control goes to the statement immediately after the catch block.


#include <iostream.h> int main() { int a,b; cout<<“Enter the values of a and b”; cin>>a; cin>>b; try { if(b!=0) { cout<<“Result(a/b)=“<<a/b; } else //there is an exception { throw(b); //throws int object } } catch(int i) //catches the exception { cout<<“exception; } cout<<“end”; return 0; }