You are on page 1of 32

Exception Handling

20/04/11

Exception Handling
It is the process of handling run-time errors in C++ code. It enables to create error-handling routines that are invoked when a run-time error is generated. Advantage of error handling is that program does not terminate when a run-time error occurs, instead the error handling mechanism allow to control the flow of program automatically

The Exception Handling Process


C++ provides three keywords for exception handling. - try - catch - throw The try keyword creates a block that contains the statements to monitor for run-time errors.

Contd..
The throw statement is used for throwing an exception. The catch block is used for catching an exception generated in the try block. The catch process is type sensitive.

Syntax of using the try, catch, & throw keywords


try { datatype exception; // statements to be monitored throw exception ; } catch(datatype argument) { // statements to be run if exception is raised }

Example
#include<iostream.h> void main() { int no; cout<<Enter number; cin>> no; cout<<endl<<endl; try { if(no==0) { throw no; }

Contd..
else { float result = (float) 100/no; cout<<when you divide 100 by<<no<<endl<<endl ; cout<<Answer is : \t<<result <<endl<<endl ; } } catch(int i) { cout <<Handling Exception <<endl <<endl ; cout <<Division by <<i<<not possible<<endl<<endl; } }

Contd..
The output when the number input by the end user is zero, is : Enter number 0 Handling Exception Division by 0 not possible Press any key to continue The output if the number input by the end user is not zero: Enter number3 when you divide 100 by 3 Answer is : 33.3333 Press any key to continue

Using Multiple Catch Statements


try { datatype exception1; datatype exception2; //statements to be monitored throw exception1; throw exception2; } catch(datatype1 arg1) { //statements to be run if exception of type // datatype1 is raised }

Contd..
catch(datatype2 arg2) { //statements to be run if exception of type // datatype1 is raised }

Example
#include<iostream.h> #include<conio.h> void main() { int no; cout<<Enter a number from 1 to 3 ; cin>>no; try { if(no == 1) throw 1; if (no == 2) throw String Type Exception;

Contd..
if( no == 3) throw 9.99; } catch (int arg1) { cout <<endl<<Integer Type Exception << arg1; } catch(char *arg2) { cout <<endl<<Float Type Exception << arg2; } }

Output
When the number, 2 ,is input by the user to raise a string type exception. Enter a number from 1 to 3 2 String Type Exception Press any key to continue

Catching all Exceptions


try { datatype exception1; datatype exception2; //statements to be monitored throw exception1; throw exception2; } catch() { //statements to be run if exception either type // i.e datatype1 or datatype2 is raised }

Example
#include<iostream.h> #include<conio.h> void main() { int no; cout<<Enter a number from 1 to 2 ; cin>>no; try { if(no == 1) throw 1; if (no == 2) throw String Type Exception; }

Contd..
catch (.) { cout <<endl<<Exception Raised << endl<<endl; }

Output
Enter a number 1 or 2 2 Exception Raised Press any key to continue

Rethrowing Exception
When an exception is generated in the try block, the control passes to the corresponding catch block. To rethrow that exception from the catch block, use the throw clause without any arguments. It is caught by the outer catch block.

Syntax..
try { datatype exception; // statements to be monitored throw exception; } catch(datatype arg1) { // statements to be run if exception is raised throw; }

Example
#include<iostream.h> #include<conio.h> void main() { int no; cout<<Enter a number from 1 to 2 ; cin>>no; try { if(no < 0) throw no; else cout <<endl<<endl<<no<<Is a possitive Number << endl; }

Contd..
catch(int arg1) { cout <<endl<<arg1<<Is a Negative Number <<endl; } catch(int arg2) { cout <<endl<<Exception Raised Again using throw<<endl <<endl; cout <<arg2<<Is a Negative Number<<endl; } }

Output
Enter a number -12 -12 is a Negative Number Exception Raised Again using throw -12 Is a Negative Number Press any key to continue

Creating Function Specific Exceptions


To control the type of exception that can be raised in a function block, can use the throw clause and specify a list of data types with the function definition. This will restrict the exception that can be raised by the function to the data types specified with the throw clause.

Syntax of using throw clause


The syntax of using the throw clause to restrict the types of exception that can be generated by a function is :
datatype functionName(datatype arg1, datatype arg2) throw( datatpe1, datatype2, datatype3) { // function statements; }

Example
#include<iostream.h> #include<conio.h> void restrictfunc(int a, int b) throw(int , char*) { int sum = a + b; if (sum == 0) throw sum; else if(sum < 0) throw Sum is negative ; else cout<<endl<< Sum of <<a<< and <<b<< is<<sum<<endl;

Contd..
void main() { int no1, no2; cout<<Enter First Number; cin>>no1; cout<<Enter Second Number; cin>>no2; try { restrictfunc(no1, no2); } catch(int arg1) { cout <<Sum is <<arg1<<endl; }

Contd..
catch(char *arg2) { cout <<endl<<arg2<<endl; } }

Output
Enter First Number -89 Enter Second Number 50 Sum is Negative Press any key to continue

Handling Memory Failure Exceptions


To allocate more than available memory to a variable then a memory failure exception can occur. To trace the memory failure exception we can use the try and catch blocks. For this we also need to use a new operator that throws a bad_alloc, which is the derived class of exception class.

Example
#include<iostream.h> #include<conio.h> #include<new> void main() { char *var; try { var = new char [1000000000]; } catch(bad_alloc &ob) { cout <<Exception raised : <<ob.what() <<\n\n; } getch(); }

Output
Exception raised : bad_alloc

THE END

Thanks .

You might also like