I have the following C++ code and it gives me a surprise. The problem is that if I throw something except re-throw inside the catch block, the program will be terminated by calling abort and give the error message in GCC4, "terminate called after throwing an instance of 'int'". If I just use "throw;" to re-throw inside the catch block, everything will be fine.
#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;
}
}