Multiple catch blocks : try catch « Exceptions « C++ Tutorial






#include <iostream.h>

int main () {
  try
  {
    char * mystring;
    mystring = new char [10];
    if (mystring == NULL) 
       throw "Allocation failure";
    
    for (int n=0; n<=100; n++)
    {
      if (n>9) 
         throw n;
      mystring[n]='a';
      
    }
  }
  catch (int i)
  {
    cout << "index " << i << " is out of range" << endl;
  }
  catch (char * str)
  {
    cout << "Exception: " << str << endl;
  }
  return 0;
}
index 10 is out of range








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