I have a try catch inside a contructor in C++ class
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"
}
}
}
When i'm creating an object of this class, it gives the error and stops terminate called after throwing an instance of 'std::invalid_argument'.
this is the case with cross compiled on ARM (with -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) ,
but on windows it catches the error properly.
When i try a sample application with the same options from cross compiling, on the board it works perfectly.
Is there any compiler setting that could be contributing to this behaviour, that i may not have noticed?
Any suggestions?
below is the sample application
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;
}
The root cause of the problem seems to be in libjson:JSONStream, which has the overloaded << & parse function that indeed throws a exception, but in their signature they have just throw(), indicating that it doesnot throws an exception.
hence when exception happens actually , the terminate is called as explained here http://www.gotw.ca/publications/mill22.htm
The solution is to change the signature of the libjson JSONStream class functions ( << & parse) - to raise a bug ticket in libjson to modify the signature of the JSONStream class functions ( << & parse)
And it works finally.
The Windows compiler seems to ignore this, but not the linux g++ compiler