woodenToaster
woodenToaster

Reputation: 274

Try block behavior with cout statements when an exception is thrown

If a try block contains cout statements before an exception is thrown in that same block, will those statements be printed to the console, or will it behave as if the try block was never executed? For example:

void foo()
{
  try
  {
    cout << "1" << endl;
    cout << "2" << endl;
    bar();               //exception thrown in this function, but caught below
  }

  catch (exception e)
  {
    cout << e.what();    //assume the message is "error"
  }
}

Would the output of this function be

1
2
error

or

error

Upvotes: 1

Views: 176

Answers (1)

NPE
NPE

Reputation: 500773

The output would be

1
2
error

The exception does not "undo" the effects of

cout << "1" << endl;
cout << "2" << endl;

Upvotes: 2

Related Questions