我在 C++ 类的构造函数中有一个 try catch
class jsonAdap
{
jsonAdap(const char *text)
{
try
{
parsejson(text);//this calls a libjson function that throws the expection throw std::invalid_argument("exception")
}
catch(std::invalid_argument)
{
cout<<"Exception captured:"<<endl"
}
}
}
当我创建此类的对象时,它给出错误并在抛出“std::invalid_argument”实例后停止终止调用。
这是在 ARM 上交叉编译的情况(使用 -O0 -g3 -Wall -c -fmessage-length=0 -pthread -Wno-reorder -std=gnu++0x -fstack-protector-all -Wno- format-contains-nul -Wno-format-extra-args -Wno-format-zero-length) ,
但在 Windows 上它会正确捕获错误。
当我尝试使用来自交叉编译的相同选项的示例应用程序时,它在板上运行完美。
是否有任何我可能没有注意到的可能导致这种行为的编译器设置?
有什么建议吗?
下面是示例应用程序
class ctest
{
public:
ctest()
{
int x = -1;
try {
cout << "Inside try \n";
if (x < 0)
{
throw std::invalid_argument("test");
cout << "After throw (Never executed) \n";
}
}
catch (std::invalid_argument &e) {
cout << "Exception Caught \n";
}
}
void test(){};
};
int main( int argc, char* argv[] )
{
cout << "Before try \n";
ctest c;
cout << "cdone "<<endl;
return 0;
}
最佳答案
问题的根源似乎在libjson:JSONStream,它有重载的<< & parse函数确实会抛出异常,但在他们的签名中他们只是throw(),表明它不会抛出异常.
因此,当实际发生异常时,将按照此处的说明调用终止 http://www.gotw.ca/publications/mill22.htm
解决方案是更改 libjson JSONStream 类函数(<< & parse)的签名 - 在 libjson 中提出错误单以修改 JSONStream 类函数(<< & parse)的签名
终于成功了。
Windows 编译器似乎忽略了这一点,但 linux g++ 编译器没有
关于c++ - 用iibjson试试,catch不行,在target Cross compiled linux board,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981882/