Use multiple catch statements : try catch « Exceptions « C++ Tutorial






#include <iostream> 
using namespace std; 
 
void f(int test) 
{ 
  try{ 
    if(test) 
       throw test;            // throw int 
    else 
       throw "Value is zero"; // throw char * 
  } 
  catch(int i) { 
    cout << "Caught One!  Ex. #: " << i << '\n'; 
  } 
  catch(char *str) { 
    cout << "Caught a string: "; 
    cout << str << '\n'; 
  } 
} 
 
int main() 
{ 
  cout << "start\n"; 
 
  f(1); 
  f(2); 
  f(0); 
  f(3); 
 
  cout << "end"; 
 
  return 0; 
}
start
Caught One!  Ex. #: 1
Caught One!  Ex. #: 2

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.








6.1.try catch
6.1.1.A simple exception handling example
6.1.2.Throwing an exception from a function called from within a try block
6.1.3.A try block can be localized to a function
6.1.4.Use multiple catch statements
6.1.5.Catch all exceptions
6.1.6.Multiple catch blocks
6.1.7.Catch 'const char * str' (string) exception
6.1.8.Use catch(...) as a default
6.1.9.Catching derived classes
6.1.10.try catch block without exception being thrown