Table of Contents
hide
Example : A simple program or Exception Handling Examples in C++ to show Exception Handling using Class and Object.
#include <iostream>
using namespace std;
class A
{
public:
int m,n,r;
public:
void input(int p,int q)
{
m=p;
n=q;
}
int process()
{
if (n==0)
{
return 0;
}
else
{
r=m/n;
return r;
}
}
};
int main()
{
A a;
try
{
int x,y,z;
cout <<"Enter two Values : ";
cin>>x>>y;
a.input(x,y);
z=a.process();
if(z==0)
{
throw 0;
}
else
{
cout<<"The division result is = "<<z;
}
}
catch (int m)
{
cout<<"Division by Zero Exception Arise";
}
catch(...)
{
cout<<"An Unexcepted General/Default Exception Arise";
}
return 0;
}
Output:
Enter two Values : 40
0
Division by Zero Exception Arise
Enter two Values : 40
4
The division result is = 10
Example : A simple program or Exception Handling Examples in C++ to show Exception Handling using the throw keyword.
#include <iostream>
using namespace std;
int main()
{
try
{
int a = 10;
int b = 0; // int b=2;
int c;
if (b == 0)
{
throw "Division by zero error";
}
else
{
c = a/b;
cout <<"The division result is = "<< c << endl;
}
}
catch(const char* msg)
{
cerr << "Exception arised: " << msg << endl;
}
return 0;
}
Output:
Error produced: Division by zero error
----------- OR -----------
#include <iostream>
using namespace std;
int main()
{
try
{
throw 'x'; //throw single character exception
}
catch (int m) //able to receive numeric exception
{
cout << "Exception Caught ";
}
return 0;
}
Output: terminate called after throwing an instance of 'char'
----------- OR -----------
#include <iostream>
using namespace std;
int main()
{
try
{
throw "msg"; //throw string exception
}
catch (int m) //able to receive numeric value
{
cout << "Exception Caught ";
}
return 0;
}
Output:
terminate called after throwing an instance of 'char const*'
----------- OR -----------
#include <iostream>
using namespace std;
int main()
{
int x ;
cout<<"Enter a value: \n";
cin>>x;
try
{
if(x == 0)
{
throw x;
cout << "This message is never executed when throw exception arise \n";
}
cout<<"The entered value is = "<<x;
}
catch (int m )
{
cout << "Throwing Exception Caught \n";
}
return 0;
}
Output:
Enter a value:
0
Throwing Exception Caught
Enter a value:
10
The entered value is = 10
----------- OR -----------
#include <iostream>
using namespace std;
double division(int m, int n)
{
if( n == 0 )
{
throw "Exception arise due to division by zero which is not occur in computer";
}
return (m/n);
}
int main ()
{
int x,y;
cout<<"Enter two values : ";
cin>>x>>y;
double z;
try
{
z = division(x, y);
cout <<endl<<"The division result is = "<<z ;
}
catch (const char* msg)
{
cerr << msg << endl;
}
return 0;
}
Output:
Enter two values : 100
0
Exception arise due to division by zero which is not occur in computer
Example : A simple program in C++ to show how to handle re-thrown Exceptions again/Nested try-catch Statement.
#include <iostream>
using namespace std;
int main()
{
try
{
try
{
throw 100;
}
catch (int x)
{
cout <<"Catch first thrown Exception";
throw; // Re-throwing an exception again
}
}
catch (int y)
{
cout <<"\nCatch the final and re-thrown Exception";
}
return 0;
}
Output:
Catch first thrown Exception
Catch the final and re-thrown Exception
Example : A simple program in C++ to show Exception Handling in Array.
#include <iostream>
using namespace std;
int main()
{
try
{
int arr[5] = {10, 20, 30,40,50},i;
cout<<"Enter the Index value for an array for an operation : ";
cin>>i;
if (i < 0 || i > 4)
{
throw ("Array Index out of bounds Exception");
}
else
{
int x = arr[i];
cout <<"The array value at given index is = "<< x << endl;
}
}
catch(const char* e)
{
cerr <<"Exception arised : " << e << endl;
}
return 0;
}
Output:
Enter the Index value for an array for an operation : 8
Exception arised : Array Index out of bounds Exception
------------ OR ------------
#include <iostream>
using namespace std;
int main()
{
try
{
int arr[5] = {10, 20, 30,40,50},i;
cout<<"Enter the Index value for an array for an operation : ";
cin>>i;
if (i < 0 || i > 4)
{
throw ("msg");
}
else
{
int x = arr[i];
cout <<"The array value at given index is = "<< x << endl;
}
}
catch(...)
{
cerr <<"Array Index out of bounds Exception arised" << endl;
}
return 0;
}
Output:
Enter the Index value for an array for an operation : 8
Array Index out of bounds Exception arised
Example : A simple program in C++ to show Multiple Catch Exception Handling.
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a value= ";
cin>>x;
try
{
if(x==0)
{
throw x;
}
else
{
throw "String Exception";
}
}
catch (const char *msg)
{
cout << "Specific Exception Caught : " << msg;
}
catch (int m)
{
cout << "Numeric Specific Exception Caught.";
}
// catch any other unexpected exception
catch (...)
{
cout << "Default/General Exception Caught\n";
}
return 0;
}
Output:
Enter a value= 0
Numeric Specific Exception Caught.
Enter a value= 8
Specific Exception Caught : String Exception
0 Comments