Professional Documents
Culture Documents
void main()
{
cout <<"start"<<endl;
try
{
cout <<"inside try
block:"<<endl;
throw 10;
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;
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;
cout <<"end";
}
-----------------------------------
----------------------------------
// Prog 04
// When exception is caught, it is
defused.
#include<iostream>
using namespace std;
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 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;
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>
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;
}
-----------------------------------
----------------------------------