C++ 抛出异常时,尝试使用cout语句执行块行为
如果在同一块中引发异常之前,try块包含cout语句,那么这些语句会打印到控制台,还是会表现为try块从未执行过?例如:C++ 抛出异常时,尝试使用cout语句执行块行为,c++,exception,try-catch,C++,Exception,Try Catch,如果在同一块中引发异常之前,try块包含cout语句,那么这些语句会打印到控制台,还是会表现为try块从未执行过?例如: void foo() { try { cout << "1" << endl; cout << "2" << endl; bar(); //exception thrown in this function, but caught below } catch
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
异常不会“撤消”异常的影响
cout << "1" << endl;
cout << "2" << endl;
cout尝试时得到了什么输出?:)
1
2
error
cout << "1" << endl;
cout << "2" << endl;