Professional Documents
Culture Documents
exception
exception
What is an Exception
Synchronous exception
Asynchronous Exception
Synchronous exception
Eg:
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
• 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