Advertisement
#include<iostream>
using namespace std;
int main()
{
int a=10, b=0, c;
try
{
//if a is divided by b(which has a value 0);
if(b==0)
throw(c);
else
c=a/b;
}
catch(char c) //catch block to handle/catch exception
{
cout<<"Caught exception : char type ";
}
catch(int i) //catch block to handle/catch exception
{
cout<<"Caught exception : int type ";
}
catch(short s) //catch block to handle/catch exception
{
cout<<"Caught exception : short type ";
}
cout<<"\n Hello";
}
Caught exception : int type
Hello
Advertisement
#include<iostream>
using namespace std;
int main()
{
int a=10, b=0, c;
try
{
//if a is divided by b(which has a value 0);
if(b==0)
throw(c);
else
c=a/b;
}
catch(...) //catch block to handle/catch exception
{
cout<<"A catch block for all types of exceptions has caught an exception";
}
}
A catch block for all types of exceptions has caught an exception
#include<iostream>
using namespace std;
class A
{
public:
char ch ='c';
int i = 1;
void function1();
void function2();
};
void A :: function1()
{
if(ch == 'c')
throw(ch);
}
void A :: function2()
{
if(i == 1)
throw(i);
}
int main()
{
A ob;
try
{
ob.function1();
}
catch(...) //catch block to handle/catch exception
{
cout<<"A catch block for all types of exceptions has caught an exception \n";
}
try
{
ob.function2();
}
catch(...) //catch block to handle/catch exception
{
cout<<"A catch block for all types of exceptions has caught an exception";
}
}
A catch block for all types of exceptions has caught an exception
A catch block for all types of exceptions has caught an exception
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement