Exception Handling
Exception Handling
Exception Handling
ti H dli
Types of Errors…
Types of Errors…
• Whenever
Whenever we write a program we may
we write a program we may
encounter two kinds of errors such as:
1.Syntax Error: It is due to poor understanding
of the language.
2L i lE
2.Logical Error: It is due to poor understanding
I i d d di
of the problem.
A
Apart from these two kinds of error, we have
f h ki d f h
some kind of a problem called exception.
Exception Handling Mechanism
Exception Handling Mechanism
• Exception
Exception Handling mechanism is used to
Handling mechanism is used to
provide the way to detect and report an
exceptional circumstance
“exceptional circumstance” so that
so that
appropriate action can be taken.
• C++ exception is basically based on three
C++ exception is basically based on three
keywords:
• try
• throw
• catch
catch
Exception Handling Mechanism(cont.…)
Exception Handling Mechanism
tryy
This keyword is used to write a block of statement
which may generate exceptions. This block is called try
block.
block
throw
If an exception is found it is thrown using throw
If an exception is found it is thrown using throw
statement.
catch
This keyword catches the expression thrown by the
throw statement from the try block and handle it
properly. This block is called catch block.
try block
Detects and throws the exception
Exception Object
catch block
Catches and handles the exception
Catches and handles the exception
Syntax:
try
{
….
throw exception
throw exception;
….
}
catch(type argument)
{….}
Things to Remember…
Things to Remember…
• When try block throws an exception the program
y p p g
control leaves the try block and enters the catch
statement of the catch block of the type of
exception matches with the argument type of catch ,
p g yp ,
then catch block is executed for handling the
exception.
• If they don
If they don’tt match the program is terminated with
match the program is terminated with
the help of abort() function which is invoked
implicitly by default.
• When no exception is detected, the control goes to
the statement immediately after the catch block i.e.
the catch block is skipped.
pp
Example
void main()
{
int a, b; else
cout<<“Enter the 2 values for throw b;;
a, b:: ”;
b ” }
cin>>a>>b;
try catc ( t)
catch(int)
{ {
if(b!=0) cout<<“Exception Caught.”;
{ }
cout<<a/b; }
}
Function Generating a try Block or
Function Generating an Exception
Most often exceptions are thrown by function
Most often exceptions are thrown by function
that are invoked from within the try block
called throw point Once an exception is
called throw point. Once an exception is
thrown to the catch block control cannot
return the throw point
return the throw point.
throw POINT
Functions that causes the Exception
try BLOCK
Invokes a function that causes the Exception.
catch Block
Catches and Handles Exception
Syntax
return type<function name><arg>
yp g tryy
{ {
…. …
…. invoking function;
throw exception …
…. }
…. catch(type arg)
} {
Handle the Exception;
}
Note: The catch block immediately follows the try block
irrespective of the location of the throw point.
Example
#include<iostream.h> int main()
{
void divide(int a, int b)
try{
{ cout<<"We are in try
bl k "
block..."<<endl;
dl
if(b!=0) //divide(10,0);
{ divide(15,5);
divide(10 0);
divide(10,0);
cout<<a/b;
} }
catch(int)
else {
throw b; cout<<endl<<"Exception Caught...";
}
return 0;
} }
O/P: We are in try block...
We are in try block
3
Exception Caught
Multiple Catch
Syntax
try
{
throw(exception object)
}
catch(type arg)
{ }
{…}
catch(type arg)
{…}
catch(type arg)
{…}
……
……
Example
#include<iostream.h> catch(int)
{
void test(int x) cout<<"INT"<<endl;
{ }
try
{ catch(char)
if(x==0) {
throw 'x'; cout<<"CHAR"<<endl;
}
else if(x==1)
{ catch(double)
throw 1.0;; {
} cout<<"DOUBLE"<<endl;
else if(x==2) }
throw x;; }
}
}
Cont…
int main()
{
t t(0)
test(0); O/P:
test(1); CHAR
test(2); DOUBLE
return 0; INT
}
In the above
In the above program , more than one catch
program more than one catch
block is there and based on the match the
appropriate block will be executed
appropriate block will be executed.
Generic Catch
Generic Catch
In some situations, we may not be able to
anticipate all possible types of exceptions and
anticipate all possible types of exceptions and
therefore may not be able to catch them. In that
case, it is possible to force a catch statement to
catch all exceptions irrespective of the type of
catch all exceptions irrespective of the type of
exception.
Syntax
y
catch(…)
{
…..
…..
}
O/P
CAUGHT ALL
Example CAUGHT ALL
CAUGHT ALL
CAUGHT ALL
void test(int x) catch(...)
{ {
tryy cout<<"CAUGHT
{ ALL"<<endl;
if(x==0) }
throw 'x';
}
else if(x==1)
{ int main()
th
throw 1.0;
10 {
} test(1);
else if(x==2) test(2);
throw x;
throw x; test(0);
} return 0;
}