Oop Unit V
Oop Unit V
exception)
Receive the error information (Catch the
exception)
Take corrective actions (Handle the
exception)
The error handling consists of two segments
Exception Handling Mechanism
The exception handling mechanism is built
upon three keywords:
Try
Isused to preface a block of statements which may
generate exceptions.
Throw
When an exception is detected, it is thrown using a
throw statement in the try block.
Catch
A catch block defined by the keyword catch catches
the exception thrown by the throw statement in the try
block and handles it appropriately.
Exception Handling Mechanism
When the try block throws
an exception the program
control leaves the try block
and enters the catch
statement of the catch
block.
If the type of object thrown
matches the arg type in the
catch statment the catch
block is executed.
Otherwise the program is
terminated with the help of
abort( ) function.
Try block throwing an exception
int main() else
{ {
int a,b; throw(x);
cout<<“enter the values of a }
and b :”; }
cin>>a; catch(int i)
cin>>b;
{
int x = a-b;
try cout<<“Exception Caught
{ : x = “ << x << “\n”;
if(x != 0) }
{ return 0;
cout<<“Result (a/x) =“
}
<< a/x;
}
Exceptions thrown by functions
Mostly
exceptions are
thrown by
functions that
are invoked from
within the try
blocks.
The point at
which the throw
is executed is
called the throw
point.
Exceptions thrown by functions
void divide(int x, int y, int z)
{
if((x-y) != 0)
{
int R = z/(x-y);
cout << “Result = “ << R << “\n”;
}
else
{
throw (x-y);
}
}
Exceptions thrown by functions
int main()
{
try
{
divide(10,20,30);
divide(10,10,20);
}
catch(int i)
{
cout << “\n Exception caught” ;
}
return 0;
}
Throwing Mechanism
When an exception is desired to be handled is
detected, it is thrown using the throw statement.
Throw statement has one of the following forms:
throw(exception);
throw exception;
throw;
The operand object exception may be of any type,
including constants.
Catching Mechanism
A catch block looks like a function definition:
catch(type arg)
}
The type indicates the type of exception that catch block
handles.
The catch statement catches an exception whose type
matches with the type of catch argument.
Multiple Catch Statements
Multiple catch statements can be associated with a
try block.
When an exception is thrown, the exception
handlers are searched for an appropriate match.
The first handler that yields the match is executed.
After executing the handler, the controls goes to the
first statement after the last catch block for that try.
Multiple Catch Statements
void test(int x) catch(char c) // catch 1
{ {
cout<<“\nCaught a character”;
try
}
{
catch(int m) // catch 2
if (x==1) throw x; {
else cout<<“\nCaught an integer”;
if(x==0) throw ‘x’; }
else catch(double d) // catch 3
if(x== -1) throw 1.0; {
cout<<“\nCaught a double”;
cout<<“\nEnd of try-block”;
}
}
} cout<<“\n End of try-catch block”;
Multiple Catch Statements
int main( ) x == 1
{ Caught an integer
cout<<“\n x = =1”; End of try-catch system
test(1);
x == 0
cout<<“\n x = = 0”;
Caught a character
test(0); End of try-catch system
cout<<“\n x = = -1”;
test(-1); x == -1
cout<<“\n x = =2”; Caught a double
test(2); End of try-catch system
return 0;
x == 2
}
End of try-block
End of try-catch system
Catch all Exceptions
Sometimes it is not possible to anticipate all
possible types of exceptions and therefore not able
to design independent catch handlers to catch
them.
A catch statement can also force to catch all
exceptions instead of a certain type alone.
Syntax:
catch (…)
{
// statements for processing all exceptions.
}
Catch all Exceptions
void test(int x) int main( )
{ {
try
cout<<“\nTesting generic
{
if (x==1) throw x; catch”;
else test(1);
if(x==0) throw ‘x’; test(0);
else
test(-1);
if(x== -1) throw 1.0;
cout<<“\nEnd of try- test(2);
block”; return 0;
} }
}
catch(…)
{
cout<<“\n Caught an
exception”;
Re-throwing an Exception
A handler can re-throw the exception caught without
processing it.
This can be done using throw without any arguments.
Here the current exception is thrown to the next enclosing
try/catch block.
Every time when an exception is re-thrown it will not be
caught by the same catch statements rather it will be
caught by the catch statements outside the try catch block.
Re-throwing an Exception
void divide(double x, double y) int main()
{ {
cout<<“Inside Function”; cout<<“\n Inside main”;
try
try
{
if(y = =0.0)
{
throw y; divide(10.5, 2.0);
else divide(20.0, 0.0);
cout<<“Division = “ <<x/y<<“\n”; }
} catch(double)
catch(double) {
{ cout<<“\n Caught double inside
cout<<“\nCaught double inside function”;
main”;
throw;
}
}
cout<<“\n End of function”; cout<<“\n End of main”;
} return 0;
}
Specifying Exceptions
It is possible to restrict a function to throw only certain
specified exceptions.
This is done by adding a throw list clause to the function
definition.
type function(arg-list) throw (type-list)
{
.......
.......
}
The type-list specifies the type of exceptions that may be
thrown.
Throwing other type of exceptions cause abnormal termination
of program.
Specifying Exceptions
void test(int x) throw (int, double)
{
if (x==0) throw ‘x’;
else
Catch(char c)
if(x==1) throw x; {
else cout<<“\n Caught a character”;
if(x== -1) throw 1.0; }
cout<<“\n End of function block”;
}
int main( )
{ Catch(int m)
try {
{ cout<<“\n Caught a integer”;
cout<<“\nTesting throw restrictions”;
}
cout<<“\n x==0”;
test(0);
Catch(double d)
cout<<“\n x==1”;
{
test(1);
cout<<“\n x== -1”;
cout<<“\n Caught a double”;
test(-1); }
cout<<“\n x== 2”;
test(2); Cout<<“\n End of try catch block”;
}
return 0;
Summary
______ are peculiar problems that a program may encounter at run
time.
Exceptions are of two types _____ and ______.
An exception is caused by a faulty statement in ____ block, which is
Syntax:
classname<type> objectname(arglist)
};
Class Templates (Example)
class vector
{
int main()
int *v;
int size; {
public: int x[3] = {1,2,3};
vector (int m) int y[3]= {4,5,6};
{
v= new int [ size = m];
vector v1(3);
for(int i=0; i<size; i++)
v[i]=0; vector v2(3);
}
vector (int * a) v1 = x ;
{ v2 = y ;
for(int i=0; i<size; i++)
v[i]=a[i];
} int R = v1 * v2 ;
int operator * (vector &y) cout<< “ R = “ << R ;
{
int sum=0; return 0;
for (int i=0; i<size; i++)
}
sum += this -> v[i] * y . v[i];
return sum;
}
}
Class Templates (Example)
const size = 3;
template<class T> T operator * (vector & y)
class vector
{
{
T * v; T sum = 0;
public:
for(int i=0; i<size; i++)
vector()
{ {
v=new T[size];
sum += this->v[i] * y. v[i];
for(int i=0; i<size; i++)
v[i] = 0; }
}
return sum;
vector(T * a)
{ }
for(int i=0; i<size; i++)
}
v[i] = a[i] ;
}
Class Templates (Example)
int main()
{
int x[3] = {1,2,3};
int y[3] = {4,5,6};
vector <int> V1;
vector <int> V2;
V1 = x;
V2 = y;
int R = V1 * V2;
cout << “R = “ << R;
return 0;
}
Class Templates with Multiple Parameters
………
}
Function Template with Multiple
Parameters
template <class T1, class T2>
void display(T1 x, T2 y)
{
cout<<x <<“ “ << y << “\n”;
}
int main()
{
display(1999, “XYZ”);
display (12.34, 1234);
return 0;
}
Overloading of Template Functions
Example:
array <int, 10> a1; // Array of 10 integers