Exception Handling
Exception Handling
Exception
• Exceptions are the run time anomalies or unusual
conditions that a program may encounter while
executing.
First run:
Enter values of a and b
20 15
Result (a/x)= 4
End
Second run:
10 10
Exception caught : x= 0
End
Throw point outside the try block
type function(arg_list) //function with exception
{
…..
throw(object); //Throws exception
…….
}
…..
…..
try
{
…… //invoke function here.
……
}
catch(type arg) //catches the exception
{
……….
………. //Handles exception here
}
Example
#nclude <iostream.h> int main()
void divide(int x, int y, int z) {
{ try
cout<<“We are inside the { cout<<“We are inside the
function”; try block”;
If((x-y)!=0) divide(10,20,30);
{ divide(10,10,20);
int R= z/(x-y); }
cout<<“Result=“<<R; catch(int i)
} {
else cout<<“caught the
{ exception”;
throw(x-y); }
} return 0;
} }
Multiple catch statements
try
{
// try block
}
catch(type1 arg)
{
//catch block1
}
catch(type2 arg)
{
//catch block2
}
………..
………..
catch(typeN arg)
{
//catch blockN
}
Multiple catch statements
#include<iostream.h>
Void test(int x)
{
try
{ if(x==1) throw x; //int
else if(x==0) throw ‘x’; //char
else if (x== -1) throw 1.0; //double
cout<<“End of try block”;
}
catch(char c) //catch 1
{ cout<<“caught a character”;}
catch(int m)
{ cout<<“caught an integer”;}
catch(double d)
{ cout<<“caught a double”;}
cout<<“End of try-catch system”;
}
Multiple catch statements
Int main()
{
Cout<<“testing multiple catches”;
Cout<<“x==1”;
Test(1);
Cout<<“x==0”;
Test(0);
Cout<<“x==-1”;
Test(-1);
Cout<<“x==2”;
Test(2); //does not throw any exception
Return 0; //and control passes to the next
//statement after last catch
}
Catch all exceptions
• In some situations, we may not be able to predict all
possible types of exceptions and therefore may not be
able to design independent catch handlers to catch
them.
• In such situations, we can force a catch statement to
catch all exceptions instead of a certain type alone.
catch(…)
{
// Statements for processing
// all exceptions
}
Example
#include<iostream> int main()
using namespace std; {
void test (int x) cout<<“testing generic catch”;
{ test(-1);
test(0);
try
test (1);
{ return 0;
if(x==0) throw x; //int }
if(x== -1) throw ‘x’; // char
if(x == 1) throw 1.0; // float
}
OUTPUT:
catch(…) // catch all testing generic catch
{ caught an exception
cout<<“caught an exception”; caught an exception
} caught an exception
}
Catching
#include<iostream.h>
Class types as Exception
#include<string.h> int main()
class error {
{ try
int err_code; {
cout<<“Press any key:”;
char *err_desc; getch();
public: throw error(99, “test exception”);
error(int c, char *d) }
{ Catch( error e)
err_code=c; {
cout<<“exception caught successfully”;
err_desc=new char[strlen (d)]; e.err_display();
strcpy(err_desc, d); }
} getch();
void err_display(void) return 0;
{ }
cout<<“Error code:”<<err_code<<“error
description:”<< err_desc;
};
OUTPUT:
Press any key
Exception caught successfully.
Error code: 99
Error description: Test exception
Rethrowing an exception
• Rethrowing causes the current exception to be
thrown to the next enclosing try/catch
sequence and is caught by a catch statement
listed after that enclosing try block.
• In such situations, we may invoke throw
without any arguments as:
throw;
Example
#include<iostream> cout<<“ end of function”;
using namespace std; }
void divide(double x, double y) int main()
{ {
cout<<“ Inside main”;
cout<<“ Inside function”;
try
try {
{ divide(10.5,2.0);
if(y==0.0) divide(20.0, 0.0);
throw y; //Throwing double }
catch(double)
else
{
cout<<“ Division= “ << x/y; cout<<“caught double inside main”;
} }
catch( double) // Catch a double cout<<“end of main”;
{ return 0;
}
cout<<“ caught double inside function”;
throw; // Rethrowing double
}
• OUTPUT:
Inside main
Inside function
Division =5.25
End of function
Inside function
Caught double inside function
Caught double inside main
End of main
• When an exception is rethrown, it will not be caught by same catch
statement or any other catch in that group.
• It will be caught by an appropriate catch in the outer try/catch
sequence only