C++ program with multiple catch

CPP program to demonstrate Multiple Catch

As we know, in exception handling we use the mechanism of try-catch block. In try block the statements are written which can generate the exceptions an to handle those exceptions catch block is written. But in this post there are multiple catch block to handle different kind of exceptions in the program.

#include <stdio.h> 
#include <conio.h>
#include <iostream.h>
using namespace std;
void test(int);
void test(int x)
{
 try
 {
  if (x == 1) throw x;
  else if (x == 0) throw 'x';
  else if (x == -1) throw 1.0;
 }
 catch(char c)
 { 
  cout << "Caught a character exception \n";
 }
 catch(int m)
 { 
  cout << "Caught an integer exception \n";
 }
 catch(double d)
 { 
  cout << "Caught a double exception \n";
 }
 cout << "\n you input " << x << " and its square is : " << x*x << "\n";
 cout << "End of try-catch system \n\n";
}
int main()
{ 
 
 cout << "Testing Multiple Catches \n";
 cout << "x == 1 \n";
 test(1);
 cout << "x == 0 \n";
 test(0);
 cout << "x == -1 \n";
 test(-1);
 cout << "x == 2 \n";
 test(2);
 return 0;
}






















Happy Coding :)

Comments

Popular posts from this blog

MultiSelection of Item in Recycler View

Upload Image From Android To Php Server

Merge Sort