2

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;
}
buddy
  • 805
  • 1
  • 15
  • 30
  • Debug with another chained `catch(...)` – Mohit Jain May 02 '16 at 11:59
  • Won't solve your problem - but you should catch exceptions by reference! See here: http://stackoverflow.com/questions/2522299/c-catch-blocks-catch-exception-by-value-or-reference – Aconcagua May 02 '16 at 12:02
  • You might want to post the sample application, too, so we can see what you did differently there... – Aconcagua May 02 '16 at 12:03
  • tried chained catch(...) no avil – buddy May 02 '16 at 12:10
  • caught exception as reference catch (std::invalid_argument &e) , still had no avail – buddy May 02 '16 at 12:11
  • 1
    @buddingspacer I knew before - just wanted to give a hint for good practice (else I would have written an answer instead...). – Aconcagua May 02 '16 at 12:35
  • 1
    Difference: you're generating the exception yourself... Have you verified that libjson has been compiled with exceptions enabled? This is not always the case in libraries compiled for embedded systems. – Aconcagua May 02 '16 at 12:45

1 Answers1

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

buddy
  • 805
  • 1
  • 15
  • 30