Vous êtes sur la page 1sur 14

ERROR (Exception) HANDLING

The exception refers to unexpected condition in a program. The unexpected conditions could be faults, causing an error which in turn causes the program to fail. The error handling mechanism of C++ is generally referred to as exception handling. Types of Exceptions
Synchronous Exception Asynchronous Exception

Synchronous Exception
The exception which occurs during the program execution, due to some fault in the input data or technique that is not suitable to handle the current class of data, with in the program are known as synchronous exception.
Example: out of range, divide by zero, overflow, underflow

Asynchronous Exception
The errors that are caused by events beyond the control of the program are called asynchronous exceptions.

Example: disk failure

Exception handling Mechanism:


The purpose of the exception handling mechanism is to provide means to detect and report an exceptional circumstance so that appropriate action can be taken.

The exception handling mechanism consists of the following task or operations:


Find the problem ( hit the exception) Inform that an error has occurred (throw the exception) Receive the error information ( catch the exceptions) Take corrective actions( handle the exceptions)

Error handling basically consists of two segments;


One to detect and to throw exceptions Other to catch the exceptions and to take appropriate actions.

Exception Handling Model:


The exception handling mechanism uses three blocks try, throw, catch. The relationship of those three exception handling model shown below

Diagram:
Try block Detects and throws an exception Exception object Catch block Catches and handles the exception

The keyword try is used to preface a block of statements (surrounded by braces) which may generate exceptions. This block of statements known as try bock. When an exception is detected, it is thrown using a throw statement in the try block. A catch block defined by the keyword catch catches the exception thrown by the throw statement in the try block, and handle it appropriately. General format: try { . // block of statements which detects and throw exception // throws an exception . } catch ( type arg) { // block of statements that handles the exception } .. }

When the try block throws an exception, the program control leaves the try block and enters the catch statement of the catch block. Exceptions are objects or variables used to transmit information about a problem. If the type of object thrown matches the arg type in the catch statement, then catch block is executed for handling the exception. If they do not match, the program is aborted with the help of the abort() function which is invoked automatically. The catch block may have more than one catch statements, each corresponding to a particular type of exception. For example if the throw block is likely to throw two exception, the catch block will have two catch statements one for each type of exception. Each catch statement is called is exception handler. When no exception is detected and thrown, the control goes to the statement immediately after the catch block. That is catch block is skipped.

Example: divide by zero #include<iostream.h> void main( ) { int x, y, z; cout<<enter the values of x, y, z; cin>>x>>y>>z; try { if(x= =y) throw y; else int d= z / ( x- y); cout<<the result of z/ (x-y )is:<< d; } catch( int y) { cout<<exception caught x-y is << x-y ; } }

RUN 1: enter the values of x , y, z 50 30 20 the result of z/ (x-y ) is: 1 RUN 2: Enter the values of x, y, z 30 30 20 exception caught x-y is : 0

#include<iostream.h> void divide ( int x, int y,int z) { cout<<we are inside the function; if(x= =y) throw (x-y); else int d= z / ( x- y); cout<<the result of z/ (x-y )is:<< d; }

void main( ) { try { cout<< we are inside the try block; divide( 10, 20, 30); // invoke divide () divide( 10, 10, 20); // invoke divide () } catch( int i) // catches the exception { cout<<exception caught x-y is : <<i; } } RUN: we are inside the try block we are inside the function the result of z/ (x-y )is: -3 we are inside the function exception caught x-y is: 0

Multiple catches with object:

To throw an exception with an object it needs to define an abstract class.


Abstract class is nothing but a class with empty data members. Class name is called as nameless object which is used to throw the exceptions

Example: #include<iostream.h> class positive { }; class negative { }; class zero { }; void greater ( int num) { if( num >0) throw positive( ); else if (num < 0) throw negative( ); else throw zero( ); }

void main( ) { int num; cout<<enter any number; cin>>num; try { greater ( num); } catch( positive) { cout<<+ve exception; } catch(negative) { cout<<-ve exception; } catch (zero) { cout<<0 exception; } }

In such circumstances, we can force a catch statement to catch all exceptions instead of a certain type alone. It should always be placed last in the list of handlers.

Catch all exception

Catch() { statement for processing all exceptions }

#include<iostream.h> Void test(int x) { Try { if(x==0) throw x; if(x==-1) throw x; if(x==1) throw 1.0; } Catch() { cout<<caught an exception \n; } }; Int main() { cout<<testing generic catch \n; test(-1); test(0); test(1); }

RETHROWING AN EXCEPTION
A handler may decide to throw the exception caught without processing it. It invokes throw without any argument
EX: throw;

#include<iostream.h> Void divide(double x, double y) { cout<<insidde function \n; try { if(y==0.0) throw y; else cout<<Division = <<x/y; catch(double) { cout<<caught double inside function \n; throw; } cout<<end of function \n ; } Int main() { cout<<Inside main; try { divide(10.5,2.0); divide(20.0,0.0); }

Catch(double) { cout<<caught double inside main \n; } Cout<<end of mai \n; }

EXCEPTION SPECIFICATIONS
It is possible to restrict a function to throw only certain specified exceptions General form type function(arg-list) throw (type-list) { function body }

#include<iostream.h> Void test(int x) throw(int double) { Try { if(x==0) throw x; else if(x==-1) throw x; else if(x==1) throw 1.0; cout<<end of function block; } }; Int main() { cout<<testing throw restrictions \n; cout<<x==-1 \n; test(-1); cout<<x==0 \n; test(0); cout<<x==1 \n; test(1); } Catch(char c) { cout<<caught a character \n; } Catch(int m) { cout<<caught an integer \n; } Catch(double d) { cout<<caught an double \n; } Cout<<end of try catch system; }