Catching mechanism

Catching mechanism :- One or more catch blocks are used to handle thrown exception.
Syntax :- catch (type arg){
                statements;
                ..............
                .............
                }

Example:-

#include<iostream.h>
#include<conio.h>
void divide(int x,int y,int z){
if ((x-y)!=0){
int R=z/(x-y);
cout<<"Result="<<R <<endl;
}
else throw(x-y);
}
void main(){
try{
divide(10,20,40);
divide(10,10,30); //invoke divide
}
catch(int i){
cout<<"Exception Caught x-y="<<i<<endl;
//throw; for rethrow
}
getch();
}

No comments:

Post a Comment