【问题标题】:Try block behavior with cout statements when an exception is thrown抛出异常时使用 cout 语句尝试块行为
【发布时间】:2013-03-28 07:25:58
【问题描述】:

如果 try 块在同一块中引发异常之前包含 cout 语句,这些语句会被打印到控制台,还是会表现得好像 try 块从未执行过?例如:

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"
  }
}

这个函数的输出是

1
2
error

error

【问题讨论】:

  • 你尝试的时候得到了什么输出? :)

标签: c++ exception try-catch


【解决方案1】:

输出将是

1
2
error

异常不会“撤销”

的效果
cout << "1" << endl;
cout << "2" << endl;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2023-03-18
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多