You are on page 1of 11

Exception Handling

It is a built in error handling


mechanism. Against traditional
error handling mechanism, using
exception handling, exceptional
situations can be handled in easier
and better way.

The exception handling is built


upon three keywords
The try keyword: It appears at the
place where measures to be taken on
errors occured. The
statements/method calls which are
to be monitored for exceptions are
contained in try block.

The catch keyword: If statements


written in try block eventually
throw exception, control is
immediately sent to matching catch
block. A catch block should
contain statements to take measure
on the exception occured. After
catch block, program resumes
execution from the statement
followed by last catch block.
The throw statement: A statement
which should be executed when an
exceptional situation occurs.
// Prog 01
// Illustrating ry-catch-throw
#include<iostream>
using namespace std;

void main()
{
cout <<"start"<<endl;
try
{
cout <<"inside try
block:"<<endl;
throw 10;

cout <<"this will not


execute:";
}

catch(int i)
{
cout <<"caught One ! number
is ";
cout <<i <<endl;
}
cout <<"end";
}

-----------------------------------
----------------------------------
// Prog 02
#include<iostream>
using namespace std;

void main()
{
cout <<"start"<<endl;
try
{
cout <<"inside try
block:"<<endl;
throw 10;

cout <<"this will not


execute:";
}

catch(int i)
{
cout <<"caught One ! number
is ";
cout <<i <<endl;
}
cout <<"end";
}

-----------------------------------
----------------------------------
// Prog 03
// Propagation of exceptions from a
layer to layer
// When exception occurs, it can be
delt at the suitable place
#include<iostream>
using namespace std;

void a(int test)


{
cout <<"inside
test:"<<test<<endl;
if (test)
throw test;
}
void main()
{
cout <<"start"<<endl;
try
{
a(0);
a(1);
a(2);
}
catch(int i)
{
cout <<"caught One ! number
is ";
cout <<i <<endl;
}

cout <<"end";
}
-----------------------------------
----------------------------------
// Prog 04
// When exception is caught, it is
defused.
#include<iostream>
using namespace std;

void a(int test)


{
try
{
if (test)
throw test;
}
catch(int i)
{
cout <<"caught one
ex :"<<i<<endl;
}
}

void main()
{
cout <<"start"<<endl;
a(1);
a(2);
a(0);
a(3);
a(4);

cout <<"end";
}

-----------------------------------
----------------------------------
// Prog 05
// The try statements may throw
different types of exceptions
As try block holds multiple
statements capable of throwing
different exceptions, there can be
multiple catch blocks to take
exception specific measures.
Exception is thrown with the
different types. The catch block
which matches with this type is
executed. There must not be two
catch blocks accepting same type of
exception.

#include<iostream>
using namespace std;

void a(int test)


{
try
{
if (test)
throw test;
else
throw "value is zero:";
}
catch(int i)
{
cout <<"caught one
ex :"<<i<<endl;
}
catch(char *str)
{
cout <<"caught a sdtring:";
cout <<str <<endl;
}

void main()
{
cout <<"start"<<endl;
a(1);
a(2);
a(0);
a(3);
a(4);

cout <<"end";
}
-----------------------------------
----------------------------------
// Prog 06
// Introducing generic exceptions

#include<iostream>
using namespace std;

void a(int test)


{
try
{
if (test==0)
throw test;
if (test==1)
throw 'a';
if (test==2)
throw 123.2;
}

catch(int i)
{
cout <<"caught one
ex :"<<i<<endl;
}
catch(...) // Generic Exceptions
{
cout <<"caught all other
exception" <<endl;
}
}

void main()
{
cout <<"start"<<endl;
a(1);
a(2);
a(0);
a(3);
a(4);
cout <<"end";
}

-----------------------------------
----------------------------------
// Prog 07
// Throwing memory exceptions.
#include<iostream>
#include<new>

using namespace std;

void main()
{
long *p;

try
{
p=new long[1000000];
}
catch(bad_alloc x)
{
cout <<"allocation failure:";
system ("pause");
return;
}

for(*p=0;*p<=10;(*p)++)
{
cout <<*p<<" "<<endl;
}

delete[] p;
}

-----------------------------------
----------------------------------

You might also like