Mega Code Archive

 
Categories / C++ Tutorial / Exceptions
 

Use multiple catch statements

#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.