Exception handling Examples program in C++




1. Program to explain Exception Handling using try, catch and throw

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
clrscr();

int a, b;
cout << "Enter value of a and b : ";
cin >> a >> b;
int c = a - b;

try
{
if( c != 0 )
{
cout << "\n Result : " << (a/c);
}
else
{
throw(c);
}
}
catch (int i)
{
cerr << "\n Exception caught : " << i;
}
cout << "\n END.";

getch();
}

output:

Enter value of a and b : 5 3

Result : 2
END.





2. Program to explain Exception Handling using try, catch and throw. ( throwing exception from function and catching in main function )

#include <iostream>
#include <conio.h>
using namespace std;

void divide(int x, int y, int z)
{
cout << "\n Inside divide().";
if( (x - y) != 0 )
{
int res = z/(x - y);
cout << "\n Result : " << res;
}
else
{
throw(x - y);
}
}

int main()
{
clrscr();

try
{
cout << "\n Inside main().";
divide(20, 30, 30);
divide(30, 10, 80);
}
catch (int i)
{
cerr << "\n Exception caught : " << i;
}
cout << "\n END.";

getch();
}

output:

Inside main().
Inside divide().
Result : -3
Inside divide().
Result : 4
END.



3. Program to explain throwing exception from function and catching in main function

#include <iostream.h>
#include <conio.h>

double division(int a, int b)
{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}

void main()
{
clrscr();

int x = 50;
int y = 0;
double z = 0;
try
{
z = division(x, y);
cout << z << endl;
}
catch (const char* msg)
{
cerr << msg << endl;
}

getch();
}

Output:

Division by zero condition!




4. Program to use Multiple Catch block

#include <iostream>
#include <conio.h>

using namespace std;

void test(int x)
{
try
{
if(x == 0)
throw 'B';
else if(x == 1)
throw x;
else if(x == 2)
throw 1.0;
cout << "\n End of Try-block.";
}
catch (char c)
{
cerr << "\n Caught a char : " << c;
}
catch (int i)
{
cerr << "\n Caught a int : " << i;
}
catch (double d)
{
cerr << "\n Caught a double : " << d;
}
cout << "\n End of Try-catch system.";
}

int main()
{
clrscr();

cout << "\n Testing Multiple Catches.";
cout << "\n\n x == 0 ";
test(0);
cout << "\n\n x == 1 ";
test(1);
cout << "\n\n x == 2 ";
test(2);
cout << "\n\n x == 3 ";
test(3);

getch();


}

output:

Testing Multiple Catches.

x == 0
Caught a char : B
End of Try-catch system.

x == 1
Caught a int : 1
End of Try-catch system.

x == 2
Caught a double : 1
End of Try-catch system.

x == 3
End of Try-block.
End of Try-catch system.





5. Program to use Generic Catch block to catch all types of Exceptions

#include <iostream.h>
#include <conio.h>

void test(int x)
{
try
{
if(x == 0)
throw 'B';
else if(x == 1)
throw x;
else if(x == 2)
throw 1.0;
cout << "\n End of Try-block.";
}
catch(...)
{
cerr << "\n Caught an exception.";
}
cout << "\n End of Try-catch system.";
}

void main()
{
clrscr();

cout << "\n Testing Generic Catch.";
cout << "\n\n x == 0 ";
test(0);
cout << "\n\n x == 1 ";
test(1);
cout << "\n\n x == 2 ";
test(2);
cout << "\n\n x == 3 ";
test(3);

getch();
}

Output:

Testing Generic Catch.

x == 0
Caught an exception.
End of Try-catch system.

x == 1
Caught an exception.
End of Try-catch system.

x == 2
Caught an exception.
End of Try-catch system.

x == 3
End of Try-block.
End of Try-catch system.






6. Program to explain rethrowing the exception
from inner catch to outer catch

#include <iostream.h>
#include <conio.h>

void divide(double x, double y)
{
cout << "\n Inside divide().";
try
{
if( y == 0.0 )
{
throw y;
}
else
{
cout << "\n Division : " << (x/y);
}
}
catch(double)
{
cerr << "\n Caught double inside divide.";
throw;
}
cout << "\n End of function.";
}

void main()
{
clrscr();
try
{
cout << "\n Inside main().";
divide(20.5, 10.5);
divide(10.0, 0);
}
catch (double d)
{
cerr << "\n Exception double inside main.";
}
cout << "\n END.";

getch();
}

Output:

Inside main().
Inside divide().
Division : 1.95238
End of function.
Inside divide().
Caught double inside divide.
Exception double inside main.
END.







7. Program to specifying exeptions in the functions throw list

#include <iostream.h>
#include <conio.h>
void test(int x)  throw(char, int, double)
{
if(x == 0)
throw 'B';
else if(x == 1)
throw x;
else if(x == 2)
throw 1.0;
cout << "\n End of Try-block.";
}

void main()
{
clrscr();
try
{
cout << "\n Testing throw.";
cout << "\n\n x == 0 ";
test(0);
cout << "\n\n x == 1 ";
test(1);
cout << "\n\n x == 2 ";
test(2);
cout << "\n\n x == 3 ";
test(3);
}
catch (char c)
{
cerr << "\n Caught a char : " << c;
}
catch (int i)
{
cerr << "\n Caught a int : " << i;
}
catch (double d)
{
cerr << "\n Caught a double : " << d;
}
cout << "\n End of Try-catch system.";

getch();
}

Output:

Testing throw.

x == 0
Caught a char : B
End of Try-catch system.


Reactions

Post a Comment

0 Comments