OOAD Miscellanous Q/A #1

Question:What is exceptional handling? Explain with examples.



Exceptions are the unusual conditions that prevent the normal execution of a program. It is a situation in which a program has an unexpected behavior that the section of code containing the problem is not designed to handle. There are two type of exceptions

  1. Synchronous Exceptions - These exceptions are caused due to errors in the programs. For example "out of range index" , "overflow" etc.

  2. Asynchronous exceptions - These exceptions are caused by events which are beyond the control of the program. Example of such an error is "input /output interrupts."

Exceptional handling

Exceptional handling refers to handling the exceptions in a systematic manner. It is a mechanism to separate code which detects and handles exceptional circumstances from the rest of the program.

Ways of Handling Exceptions

The exceptions can be handled in following ways:

  • Terminate the execution of the program. This may cause loss of data and damages. This is not a good way and thus should be avoided.

  • Simply display the error message on the screen and proceed further.

  • Call exit() function to indicate a normal exit after successfully completing the task.

  • Call abort ( ) function to terminate the program execution abruptly.

  • Handle the execution using standard C++ features. This is the best way.

Purpose of exception handling mechanism:

The purpose of exception handling mechanism is to provide a method in order to detect and report an exceptional circumstances so that right action can be taken. It involves the following tasks:

  1. Detects the error which is called "Hit" the exception.

  2. Informs that an errors has occurred. It is called "throw" the exception.

  3. Receives the errors information termed as "catch" the exception.

  4. Takes corrective actions termed as "Handle" the exception.

Thus error handling code has two parts:

  • To detect the errors and throw exceptions.

  • To catch exceptions and to take appropriate actions.


The C++ language has provided the following keywords to deal with exceptions. These three keywords are used to handle exceptions.

  • Try

  • Throw

  • catch

Program example for exception handling

#include <iostream>

using namespace std;

void divide (float dividend, float divisor );

int main()
   int a,b;
   cout<<"Enter two numbers"<<endl;
   cout<<"3 0"<<endl;
      divide (3,0);
   catch (float p)
      cout << "exception occurred "<<endl;
      cout<<"value of divisor ="<<p;
   return 1;
void divide (float dividend , float divisor)
   if (divisor  ==0)
      throw (divisor );
      cout <<"output ="<<dividend /divisor;


Enter two numbers
3 0
Exception occurred 
Value of divisor = 0
Kickstart Your Career

Get certified by completing the course

Get Started