在我希望它们被捕获的情况下,异常不会被捕获。该代码位于 1 个 cpp 文件中的 1 个函数中,该文件由 GCC 4.2 编译为静态库,然后链接到 Cocoa 应用程序中。有问题的代码是
class runtime_error : public exception{
// More code
};
int foo( void ){
try {
if( x == 0 ){
throw std::runtime_error( "x is 0" );
}
}
catch( std::exception & e ){
// I expect the exception to be caught here
}
catch( ... ){
// But the exception is caught here
}
}
我可以将代码修改为
int foo( void ){
try {
if( x == 0 ){
throw std::runtime_error( "x is 0" );
}
}
catch( std::runtime_error & e ){
// exception is now caught here
}
catch( … ){
}
}
代码的第二个版本只解决了 runtime_error 异常的问题,而不解决可能从 std::exception 派生的其他异常类的问题。知道有什么问题吗?请注意,代码的第一个版本适用于 Visual Studio。
谢谢,
巴里