Unit V Exception
Unit V Exception
Exceptions: Exceptions are runtime anomalies or unusual conditions that a program may
encounter while executing .Anomalies might include conditions such ass division by zero,
accessing an array outside of its bounds or running out of memory or disk space. When a
program encounters an exception condition, it must be identified and handled.
Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.
1.Synchronous exceptions
2.Asynchronous exceptions
1.Synchronous exceptions:Errors such as “Out-of-range index” and “over flow” are synchronous
exceptions
2.Asynchronous exceptions: The errors that are generated by any event beyond the control of
the program are called asynchronous exceptions
The purpose of exception handling is to provide a means to detect and report an exceptional
circumstance
An exception is said to be thrown at the place where some error or abnormal condition is
detected. The throwing will cause the normal program flow to be aborted, in a raised exception.
An exception is thrownprogrammatic, the programmer specifies the conditions of a throw.
In handled exceptions, execution of the program will resume at a designated block of code,
called a catch block, which encloses the point of throwing in terms of program execution. The
catch block can be, and usually is, located in a different function than the point of throwing.
C++ exception handling is built upon three keywords: try, catch, and throw.
Try is used to preface a block of statements which may generate exceptions. This block of
statements is knownas try block. When an exception is detected it is thrown by using throw
statement in the try block.
Catch block catches the exception thrown by throw statement in the try block and handles it
appropriately.
The structure ofthese two blocks is shown below:
......
......
try
......
.....
catch(type arg)
......
......
......
When the try block throws an exception, theprogram control leaves the try blockand enters the
catch statement ofthe catch block. Here exception is nothing but an object used to transmit
theinformation about a problem. If the type of object thrown matches the arg typeof the catch
statement then catchblock is executed for handling the exception.
A program is givenbelow that will explain you that how actually exception handling is done in
C++.
#include<iostream>
int main()
int x,y,z;
cin>>x>>y;
z=x-y;
try
if(z!=0)
cout<<“Result(x/z)=”<<x/z;
{
cout<<“Exception caught: z=”<<z;
return 0;
A try block can be followed by one or more catch blocks. Each catch block must contain a
different exception handler. So, if you have to perform different tasks at the occurrence of
different exceptions.
try
{
//try block
//catch block1
//catch block2
………………
……………..
//catch blockN
• When an exception is thrown, the exception handler are searched in order fore an appropriate
match.
• It is possible that arguments of several catch statements match the type of an exception. In
such cases the first handler that matches the exception type is executed
void test(int x)
try
if(x==1) throw x;
else
else
catch(char c)
cout<<"caught a character"<<endl;
catch(int m)
cout<<"caught an integer"<<endl;
catch(double d)
cout<<"caught a double"<<endl;
}
int main()
test(1);
test(0);
test(-1);
test(2);
return 0;
Output:
caught an integer
caught a character
caught a double
All possible types of exceptions and therefore may not be able to design independent catch
handlers tocatch them. In such circumstances, we can force a catch statement to catch all
exceptions instead of a certain type alone.
catch(…)
………
#include<iostream.h>
void test(int x)
{
try
if(x==0) throw x;
catch(...)
cout<<"caught exception"<<endl;
int main()
test(-1);
test(0);
test(1);
return 0;
Re-throwing an Exception:
It is possible to pass exception caught by a catch block again to another exception handler. This
is known as Re-throwing.
#include <iostream>
void MyHandler()
{
try
throw "hello";
int main()
try
MyHandler();
catch(const char*)
return 0;
Specifying Exceptions:
Specification of exception restrict functions to throw some specified exceptions only with the
use of throw(exception list) in the the header of the function.
General form
Statements
try
statements
Example Description
after coming back from the functions, pops out the address to start the
void f3() {
work where it was left of.
cout<<"\n f3() Start ";
The stack unwinding is a process where the function call stack entries
try {
are removed at runtime. To remove stack elements, we use exceptions.
If an exception is thrown from the inner function, then all of the entries
f2();
of the stack is removed and return to the main invoker function.
}
So exception handling involves Stack Unwinding if exception is not
exception handler. The next entry is f2(). Since f2() also doesn’t have
int main() {function
// handler
f3();
void handler() {
}cout<<"In the handler()\n"; }
handler, its entry is also removed from function call stack. The next entry in
function call stack is f3(). Since f3() contains exception handler, the catch
block inside f3() is executed, and finally the code after catch block.
Standard Exceptions :
In C++ standard exceptions are defined in <exception> class that we can use inside our
programs. The arrangement of parent-child class hierarchy is shown below:
1 std::exception
2 std::bad_alloc
3 std::bad_cast
4 std::bad_exception
6 std::logic_error
7 std::domain_error
8 std::invalid_argument
9 std::length_error
10 std::out_of_range
This#include
is thrown<iostream>
if a mathematical overflow occurs.
#include<exception>
13 std::range_error
int main()
using namespace std;
This is occurred { when you try to store a value which is ou
class ZeroException :public exception
14 std::underflow_error try
{
This is thrown {if a mathematical underflow occurs.
public:
int a,b;
const char* what()
Exception Objects
cout<<"Enter a and b:"<<endl;
The C++ Standard{ library provides a base class specifically designed to declare objects to be
cin>>a>>b;
thrown as exceptions.It is called std::exception and is defined in the <exception> header.
{
};
ZeroException obj;
throw obj;
else
cout<<"a/b = "<<a/b<<endl;