Using Multiple catch Statements : try catch « Language « C++






Using Multiple catch Statements

Using Multiple catch Statements
 
#include <iostream>
using namespace std;

void myFunction(int test)
{
  try{
    if(test) throw test;
    else throw "Value is zero";
  }
  catch(int i) {
    cout << "Caught Exception #: " << i << '\n';
  }
  catch(const char *str) {
    cout << "Caught a string: ";
    cout << str << '\n';
  }
}
int main()
{
  cout << "Start\n";
  myFunction(1);
  myFunction(2);
  myFunction(0);
  myFunction(3);
  cout << "End";
  return 0;
}

           
         
  








Related examples in the same category

1.Use multiple catch statements. Use multiple catch statements.
2. Localize a try/catch to a function. Localize a try/catch to a function.
3.Code block of try...catch
4.Catch more than one type of exceptions
5.Catch different types of exception
6.Catch exception from a function
7.Catch char pointer type exception
8.Catch int type exception in a function
9.Catch all types of exceptions
10.Catch By Non Const Reference
11.Catch By Value
12.Checking for a divide-by-zero exception.
13.Finally catch all Exceptions