Professional Documents
Culture Documents
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
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.
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
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
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
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
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 .