Throwing an exception from a function outside the try block : function and Exception « Exceptions « C++ Tutorial






#include <iostream>
using namespace std;

void f(int test)
{
  cout << "Inside f, test is: " << test << "\n";
  if(test) throw test;
}

int main()
{
  cout << "Start\n";

  try { // start a try block
    cout << "Inside try block\n";
    f(0);
    f(1);
    f(2);
  }
  catch (int i) { // catch an error
    cout << "Caught an exception -- value is: ";
    cout << i << "\n";
  }

  cout << "End";

  return 0;
}
Start
Inside try block
Inside f, test is: 0
Inside f, test is: 1
Caught an exception -- value is: 1
End"








6.6.function and Exception
6.6.1.Localize a try/catch to a function
6.6.2.Throwing an exception from a function outside the try block
6.6.3.This function can throw NO exceptions!
6.6.4.Restricting function throw types