0% found this document useful (0 votes)
11 views

C++ Exception Handling

Uploaded by

Nischay Singh
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)
11 views

C++ Exception Handling

Uploaded by

Nischay Singh
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
You are on page 1/ 16

Devesh Lowe

 Object Oriented Programming with C++, E


Balagurusamy, Tata McGraw Hill
 Mastering C++, K R Venugopal, Rajkumar
Buyya, T Ravishankar: TATA McGraw Hill
 Exceptions are run time anomalies or unusual
conditions that a program may encounter
while executing.
 These are situations which may lead to
abnormal termination of program as these
generally occur at run-time. (syntax or
semantic errors are traced before that)
 Exception include errors like accessing array
element out of bound, division by zero,
running out of memory, lack of rights or
previledges to access a file or resource etc.
 Exceptions are synchronous and asynchronous
exceptions.
 Errors such as “overflow” or “out of range index”
belong to synchronous exception.
 Errors that occur beyond the control of program
are called asynchronous exceptions.
 In C++ we can handle only synchronous
exceptions.
 The purpose of exception handling mechanism is
to provide means to detect and report an
exceptional circumstance so that appropriate
action can be taken.
 The mechanism provides a separate error
handling code to be added to perform :
◦ Find the problem (HIT)
◦ Inform that error has occurred (THROW)
◦ Receive the error information (CATCH)
◦ Take corrective action (HANDLE)
 It is built on three keywords namely try, throw
and catch.
 Try is used to preface a block of statement with
probability for error generation.
 When error is detected it throws it.
 A catch block follows try block and tries to catch
the exception thrown by try block.
 Catch block immediately follows the try
block.
 We can have multiple catch blocks depending
on the possibility of different types of
exceptions in one try block.
 Exceptions are the objects which may contain
error information. Exception type must match
the argument in catch block.
 As soon as exception is raised, control leaves
try block and enters matching catch block
and executes the code in it.
 If it does not find a matching catch block
then program is aborted using abort()
function which is by default invoked.
 When no exception is detected, control is
transferred immediately to the next
statement after catch block.
 A catch(…) block catches all the exceptions
raised and is therefore added at the end.
void test(int x)
{ try
{
if( x==1) throw x;
else if(x==0) throw 'x';
Multiple catch
else if(x==-1) throw 1.0;
blocks
cout<<" \n end of try block ";
}
catch(char c)
{
cout<<"\n caught character exception ";
}
catch(int i)
{
cout<<"\n caught integer exception ";
}
catch(float f)
{
cout<<"\n caught float exception ";
}

cout<<"\n end of try catch statement ";


}
void divide(int x, Int y, int z)
{
cout<< "\n we are inside function ";
try
{
if( (x-y) != 0)
{
int R=z/(x-y);
cout<<" Result = "<<R<<"\n";
}
else
{ throw(x-y); }
}
catch( int i )
{ cout<<" caught exception "; }
}

int main()
{
cout<<" we are inside try block ";
divide(10,20,30);
divide(10,10,30);
}
int main()
{
cout<<" testing multiple catches \n ";
cout<<"\n x=1 ";
test(1);
cout<<"\n x=0 ";
test(0);
cout<<"\n x=-1 ";
test(-1);
cout<<"\n x=2 ";
test(2);
return 0;
}
void test(int x)
{
try
{
if( x==1) throw x;
else if(x==0) throw 'x';
else if(x==-1) throw 1.0;

cout<<" \n end of try block ";


}
catch(...)
{
cout<<"\n caught an exception ";
}

cout<<"\n end of try catch statement ";


}
 A catch handler may decide to rethrow the
exception caught with out processing it. In
such situation, we may simply invoke throw
without any arguments.
 This causes the current exception to be
thrown to the next enclosing try/catch block
sequence and is caught by catch statement
listed after the enclosing try block.
void divide(double x, double y)
{
cout<<"\n inside functions ";

try
{
if( y==0.0)
throw y;
else
cout<<"\n division = "<<x/y;
else if (x==-1) throw 1.0;
}
catch(double)
{
cout<<"\n caught double inside function ";
throw;
}
cout<<"\n end of function ";
}
int main()
{
cout<<" inside main \n ";
try
{
divide(10.5,2.0);
divide(20.0,0.0);
}
catch(double)
{
cout<<"\n caught double inside main ";
}
cout<<"\n end of main code ";
return 0;
}
 It is possible to restrict a function to throw
only a specified type of exception.
 This is achieved by adding a throw list clause
to the function definition.
 The type list specifies the types of exceptions
that can be thrown.
 Throwing any other type of exception will
cause abnormal termination of program.
 If we wish to prohibit a function from
throwing any exception we can mention
throw(); in the function header.
void test(int x) throw( int, double)
{
cout<<"\n inside functions ";
if( x==0) throw 'x';
else if(x==1) throw x;
else if(x==-1) throw 1.0;
cout<<"\n end of function ";
}

You might also like