0% found this document useful (0 votes)
5 views24 pages

Exception Handling in Programming

The document provides an overview of exception handling in programming, detailing the types of errors and exceptions, as well as the mechanisms for handling them using keywords like try, throw, and catch. It explains synchronous and asynchronous exceptions, the process of catching exceptions, and the ability to re-throw exceptions or specify which exceptions a function can throw. Additionally, it discusses the use of multiple catch statements and the advantages of using exception handling to prevent program termination.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

Exception Handling in Programming

The document provides an overview of exception handling in programming, detailing the types of errors and exceptions, as well as the mechanisms for handling them using keywords like try, throw, and catch. It explains synchronous and asynchronous exceptions, the process of catching exceptions, and the ability to re-throw exceptions or specify which exceptions a function can throw. Additionally, it discusses the use of multiple catch statements and the advantages of using exception handling to prevent program termination.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXCEPTION HANDLING

Contents
 Errors and Exception

 Exception Handling Mechanism


 Try, Throw and Catch

 Re-throwing an Exception

 Specifying Exceptions
 What is an error?
 An error is a term used to describe any issue that arises
unexpectedly and results in incorrect output.
 What are the different types of errors?
 Logical error:
Occur due to poor understanding of problem or solution
procedure.
 Syntactic error:
Arise due to poor understanding of the language itself.
 What is an exception?
 Exceptions are run time anomalies or unusual conditions
that a program may encounter while executing.
Exception Handling
 Exceptions are of two types:
 Synchronous exceptions
 The exceptions which occur during the program
execution due to some fault in the input data are known
as synchronous exceptions.
For example: errors such as out of range, overflow,
underflow.
 Asynchronous exceptions.
The exceptions caused by events or faults unrelated
(external) to the program and beyond the control of the
program are called asynchronous exceptions.
For example: errors such as keyboard interrupts,
hardware malfunctions, disk failure.
Exception Handling Mechanism
 Exception handling mechanism provides a
means to detect and report an exception
circumstances.
 Find the problem (Hit the exception)

 Inform that an error has occurred (Throw the

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
 It is used 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;
cin>>b;
}
int x = a-b; catch(int i)
try {
{
cout<<“Exception Caught
if(x != 0)
{ : x = “ << x << “\n”;
cout<<“Result (a/x) =“
<< a/x; }
} return 0;
}
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)

// statements for managing exceptions.

}
 The type indicates the type of exception that catch block
handles.
 The catchstatementcatches 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, 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
try {
{ cout<<“\nCaught a character”;
if (x==1) }
throw x; catch(int m) // catch 2
else {
if(x==0) cout<<“\nCaught an integer”;
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);
cout<<“\n x = = 0”; 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
{
catch”;
if (x==1) throw x; else
if(x==0) throw ‘x’; else test(1);
if(x== -1) throw 1.0; test(0);
cout<<“\nEnd of try-
test(-1);
block”;
} test(2); 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”; try cout<<“\n Inside main”;
{ try
if(y = =0.0) throw y;
{
else
cout<<“Division = “ <<x/y<<“\n”;
divide(10.5, 2.0);
} divide(20.0, 0.0);
catch(double) }
{ catch(double)
cout<<“\nCaught double inside function”; {
throw; cout<<“\n Caught double inside
} main”;
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 Caught a double”;
cout<<“\n x== -1”;
}
test(-1); cout<<“\n x== 2”;
test(2);
}
Cout<<“\n End of try catch block”;

return 0;
Questions and Answers
 What is an exception?
 Exceptions are run time anomalies or unusual conditions
that a program may encounter while executing.
How is exception handled in C++?
 In C++ the exception is handled using the three
keywords try, throw and catch. Or try-catch mechanism.
What are the advantages of using
exception handling mechanism in a program?
 The purpose of exception handling mechanism is to
provide a means to detect and report an exceptional
circumstances so that appropriate action can be taken
and prevent abnormal termination of program.
Questions and Answers
 When should a program throw an exception?
 There are some situation when a program come across
unexpected errors and cause abnormal termination of
program. To handle such errors and prevent program
from termination exceptions are thrown and handled.
 What should be placed inside the try block?
 The statement that may generate an exception are
placed in the try block.
 When do we use multiple catch handlers?
 Multiple catch handlers are used in a situation where a
program has more than one condition to throw and
exception.

You might also like