다음 C++ 코드가 있는데 놀랍습니다. 문제는 catch 블록 안에 re-throw 이외의 것을 던지면 프로그램은 abort를 호출하고 GCC4에서 오류 메시지를 내고 " 'int'인스턴스를 throw 한 후 종료됩니다."라는 메시지가 표시됩니다. 방금 "throw;"를 사용하면 catch 블록 안에 다시 던져 넣으면 모든 것이 잘될 것입니다.C++에서 catch 블록 안에 뭔가를 던지면 왜 종료가 발생합니까?
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
int main()
{
try{
throw std::string("first throw");
}
catch(std::string &x){
try{
std::cout << x << std::endl;
// throw; // if I use this line, all is fine.
throw int(2); // but if I use this line, it causes Abort() to be called
}
catch (int &k){
throw;
}
catch(...)
{
cout << "all handled here!"<< endl;
}
}
catch(...){
std::cout<< "never printed" << endl;
}
}
캐치에서 던지면 예상됩니다. – AJG85