Module - 5 - Exception Handling - Lecture Notes 17181774532320
Module - 5 - Exception Handling - Lecture Notes 17181774532320
INTRODUCTION
The common types of error in the programs would be logical errors and syntactic errors. The logic errors occur
due to poor understanding of the problem and syntactic errors arise due to the poor understanding of the
programming language. These errors can be found out by debugging and testing procedures. Other than logic
and syntax errors there are certain problems that occur during program execution these are known as
exceptions.
Exceptions are run time anomalies or unusual condition that a program may encounter while executing.
Anomalies might include conditions such as division by zero, access to an array outside its bounds or running
out of memory or disk space.
C++ exception handling uses three keywords try, throw and catch. Try block contains the statements that
generate the exceptions. When an exception is detected, it is thrown using a throw statement in the try block. A
catch block catches the exception thrown by the throw statement in the try block and handles it appropriately.
The relationship between the blocks is shown in the above figure.
The catch block that catches an exception must immediately follow the try block that throws the
exception. The general form of these 2 blocks are as follows:
When the try block throws an exception the program control leaves the try block and enters the catch statement
of the catch block. Exceptions are objects that are used to transmit information about a problem. If the type of
object thrown matches the arg type in the catch block then the catch block is executed for handling the
exception. If they do not match the program is aborted with the help of the abort( ) function which is invoked
by default. When no exception is detected and thrown the control goes to the statement after the catch block.
THROWING MECHANISM
When an exception that is desired to be handled is detected, it is thrown using the throw statement in one of the
following forms:
throw (exception);
throw exception;
throw;
The operand object exception may be of any type, including constants. It is also possible to throw objects not
intended for error handling.
When an exception is thrown, it will be caught by the catch statement associated with the try block, which
means the control exits the current try block and is transferred to the catch block after that try block.
CATCHING MECHANISM
Catch block handles the exception. A catch block looks like a function definition and is of the form:
catch (type arg)
{
// Statements for managing exceptions
}
The type indicates the type of exception that catch block handles. The exception-handling code is placed
between two braces. The catch statement catches the exception whose type matches with the type of catch
argument. When it is caught the code in the catch block is executed.
If the parameter in the catch statement is named, then the parameter can be used in the exception-
handling code.
Output:
Testing Multiple Catches
x == 1
Caught an integer
End of try-catch system
x == 0
Caught an character
End of try-catch system
x == -1
Caught an double
End of try-catch system
x == 2
End of try-block
End of try-catch system
Working of the Program: The program when executed first, invokes the function test( ) with x = 1 and
therefore throws x an int exception. This matches the type of parameter m in catch2 and catch2 handler is
executed. After this execution test () with x=0 is invoked, so throw x is a character exception and catch1
handler is executed similarly test () with x = -1 and x=2 is exceuted and the cprresponding handlers are
executed.
Output:
Testing Generic Catch
Caught an exception
Caught an exception
Caught an exception
try
{
//generate a random index and test repeatedly 10 times
while(1)
{
cout<<"Enter the index of element in the array : ";
cin>>iDx;
if(iDx < 0 || iDx >=10)
{
cout << "Generated index " << iDx << " is invalid" << endl;
throw logic_error("Array out of Bounds");
}
cout << "Given index is " << iDx << " the value at that index is : " << values[iDx] << endl;
}
}
catch (logic_error& e){
cout << "Processing error " << endl << e.what() << " exception occured.\n" << endl;
}
return 0;
}
Rethrowing An Exception
A handler may decide to rethrow the exception caught without processing it. In such situation we can invoke
throw without any arguments:
throw;
This 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.
Program: Rethrowing An Exception
#include <iostream>
using namespace std;
void divide (double x, double y)
{
cout << “Inside function \n”;
Output:
Inside main
Inside function
Division = 5.25
End of function
Inside main
Caught double inside function
Caught double inside main
End of function
When an exception is rethrown it will not be caught by the 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.
A catch handler itself may detect and throw an exception. The exception thrown will not be caught by
any catch statements in that group. It will be passed to the next outer try/catch sequence for processing.
Reshma, Dept of CSE
MODULE 5: EXCEPTION HANDLING