Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

EXCEPTION HANDLING

What is an Exception

• Exceptions are error that occur during run


time. (Run time anomalies)
Exception Handling

• It is a way of transferring the control and


information to a caller that handle
exception of a given type.
Use of Exception Handling

• Throwing an exception allows user to


gather information about the error that can
be used for finding which led to the error.
Exception Types

Synchronous exception
Asynchronous Exception
Synchronous exception
Eg:

• Out of range(array index)


• Overflow
• Underflow
• Divide By Zero
Asyncronous Exception

Eg:

• Keyboard interrupts
• Hardware Malfunction
• Disk failure
Exception handing constructs
• Try
• Catch
• Throw
• Exception class
Try Construct
It defines a boundary with which an
exception occur.
Eg:
Try
{
//code raising exception
}
Catch construct
• It has an exception handler
• It should be immediately followed

by try block.
• There can be more than one catch for
a try block
• The exception handler in catch block

will be invoked with respect to their


throw expression.
Throw construct
• It is used to raise an exception when an error is
encountered.

• It calls a exception class constructor with nameless


temporary object/catch.

• The constructor will be called with arguments also

• All the thrown exception should be caught if not then


terminate function will be called which intern calls the
library function abort()
Exception class
#include<iostream> main()
using namespace std; {
class Aclass try
{ {
public: Aclass obj1;
class Anerror obj1.func();
{}; }
void func() catch(Aclass::Anerror)
{ {
if(/*error condition"*/) cout<<"exception";
}
throw Anerror(); }
}
};
Example program for exception
#include<iostream> main()
using namespace std; {
class number number num1,num2;
{ int num; int result;
public: num1.read();
num2.read();
void read()
try
{cin>>num;}
{
class DIVIDE {}; result=num1.div(num2);
int div(number n2) }
{ catch(number::DIVIDE)
if(n2.num==0) {
throw DIVIDE(); cout<<"Exception:DIVIDE
else BYZERO";
return num/n2.num;
}}; }
Exception with arguments
#include<iostream> DIVIDE(string str,int nu)
#include<string> {
using namespace std;] name=str;
class number n=nu;
{int num
}};
public:
void read() int div(number n2)
{cin>>num;} {
class DIVIDE if(n2.num==0)
{public: throw
string name; DIVIDE("divfunction",n2.num);
int n; else
return num/n2.num;
}};
Contd..
main()
{
number num1,num2;
int result;
num1.read();
num2.read();
try
{
result=num1.div(num2);
}
catch(number :: DIVIDE D)
{
cout<<"Error occured"<<D.name;
cout<<"The value is"<<D.n;
}
}
Catch all exception
Catch(…)
{
}
• The three dots indicate that it catches all
types of exception raised in its preceding
try block
Example for multiple exception
#include<iostream> main()
using namespace std; {
const int ARR_SIZE=10; try{
class array array a1(5);
{ a1[3]=10;
public: cout<<"suceeded";
int *arr; int size; cout<<"accessing a1[15]";
class SIZE{}; class RANGE{}; a1[15]=10;
array(int sizereq) }
{ if((sizereq<0)||(sizereq>ARR_SIZE)) catch(array::SIZE)
throw SIZE(); {cout<<"SIZE EXCEEDS
size=sizereq; ALLOWABLE LIMIT"<<endl;}
arr=new int[size]; } catch(array::RANGE)
~array(){delete arr;} {cout<<"ARRAY REFERENCE OUT
OF RANGE“;
int & operator [](int i)
}
{ if(i<0||i>size)
}
throw RANGE();
return arr[i]; }};
List of Exception
• A user can specify a list of exception that a function can throw.

• functionname throw(type id1,type id2 ..){ }

eg: void f2()throw(DIVIDE){}

• If any exception other that DIVIDE is thrown by f2() then control will be
transferred to predefined unexpected function even though there is catch
block for that throw.
Exception in a no-exception
function
• Eg:
Void func() throw()
{
}
Since the exception are not specified the
control is transferred to library function
abort()
Handling uncaught exception
For exceptions raised and not handled in
general:
• terminate()—calls abort()
• set_terminate()
For exceptions not in throw list:
• unexpected()—calls abort()
• set_unexpected()
Exception in inheritance
#include<iostream> class son:public father
using namespace std; {
class WRONG_AGE protected:
{}; int sage;
class father public:
{ son(int n,int m):father(n)
protected: {
int fage; if(m>=n)
public: throw WRONG_AGE();
father(int n) sage=m;
{ }
if(n<0) int getage()
throw WRONG_AGE(); {
fage=n; return sage;
} }
virtual int getage() };
{
return fage;
}
};
Contd..
main() try
{ {
int fatherage; ptr=new son(fatherage,sonage);
int sonage; }
father *ptr; catch(WRONG_AGE)
cin>>fatherage; {
try cout<<"Father age should not be less that
{ sons";
ptr=new father(fatherage); exit(1);
} }
catch(WRONG_AGE) cout<<"sons age"<<ptr->getage()<<endl;
{ delete ptr;
cout<<"father age is less than zero"; }
exit(1);
}
cout<<ptr->getage()<<endl;
delete ptr;
cin>>sonage;
Thank you

You might also like