7 Replies Latest reply on Apr 22, 2015 8:34 PM by KurtE

    Eclipse - how to enable exceptions (error handling) ?

    romankubanek

      Hi to all,

      I have this problem: I need handle error in some method like this:

      try

         {

           BadFunction();

         }

         catch (...)

         {

           cout << "Error bla bla.. ";

         }

       

      But Eclipse (Version: Luna SR1 (4.4.1)) not catch this error, and program is terminated (of course). Does anybody know how to set eclipse to accept exceptions handling?

       

      Thanks for responses

       

      Roman

        • 1. Re: Eclipse - how to enable exceptions (error handling) ?
          Intel_Peter

          Hello romankubanek,

           

          As you can see in C Language Keywords, the functions try and catch are not implemented in C, because C itself doesn't support exceptions, which if I'm not mistaken is the language you are trying use, right? However there are other ways you can simulate them like the one you can find here: Try catch statements in C - Stack Overflow and here: Exceptions in C with Longjmp and Setjmp.

           

          Peter.

          • 2. Re: Eclipse - how to enable exceptions (error handling) ?
            romankubanek

            Hi Peter,

            Thanks for your advice, it can be useful and I try it. I'm using C++ and C#. Problem is that in Windows (Visual studio) C++ can handle exceptions, but the same code not work in Eclipse IDE. For example in Visual studio is option in project settings where you can setup exceptions to work. I supposed that some similar option exists in Eclipse. So I'm still looking for solution.

             

            Thanks

             

            Roman

            • 3. Re: Eclipse - how to enable exceptions (error handling) ?
              KurtE

              I have not tried the Try/Catch stuff with my Edison code base, but have used some of the built in signal handlers...

              #include <signal.h>
              void SignalHandler(int sig){
                  printf("Caught signal %d\n", sig);
              
                  // Free up the servos if they are active
                  if (g_InControlState.fRobotOn)
                      g_ServoDriver.FreeServos();
              
                  // Additional cleanup?
                  g_ServoDriver.Cleanup();
              
                 exit(1);
              
              }
              
              
              //--------------------------------------------------------------------------
              // SETUP: the main arduino setup function.
              //--------------------------------------------------------------------------
              void setup()
              {
              #ifdef OPT_SKETCHSETUP
                  SketchSetup();
              #endif
                  g_fShowDebugPrompt = true;
                  g_fDebugOutput = false;
              #ifdef DBGSerial
                  DBGSerial.begin();                            // Special version for stdin/stdout
              #endif
              
                  // Install signal handler to allow us to do some cleanup...
                  struct sigaction sigIntHandler;
              
                  sigIntHandler.sa_handler = SignalHandler;
                  sigemptyset(&sigIntHandler.sa_mask);
                  sigIntHandler.sa_flags = 0;
              
                  sigaction(SIGINT, &sigIntHandler, NULL);
              
              
              
              }
              

               

               

              This handler is sort-of brute force and it tries to release the servos before it exits.  In other instances when I use the signal handler, the code may simply set a flag and return

              and the mainline senses the flag and exits...

               

              Not sure if that approach will work for you or not.  The above code is an extract from my Edison_Eclipse project up on github

              • 4. Re: Eclipse - how to enable exceptions (error handling) ?
                romankubanek

                Hi Kurt,

                Thanks for response, unfortunatelly I'm not sure if it solve my problem. So I try set my question by other way

                Is there some solution (C++) for handle this code without aplication crash:

                 

                void Test()

                {

                  int z = 10 /0;

                }

                • 5. Re: Eclipse - how to enable exceptions (error handling) ?
                  KurtE

                  Hi again,  While I currently don't need this for what I am doing, I was curious.

                   

                  I have not done fully what you are needing here, but did find this link on the net: Random Programming Things: Catching null pointer exceptions with GCC on Linux.

                   

                  I did verify through test program using Eclipse to the Edison, that there program works as they mentioned.   As they mentioned it was a two part fix, which includes setting up the signals like they show, plus adding the compile option:  -fnon-call-exceptions

                   

                  Again their code is specific for NULL pointer.  Not sure how much would need to change to handle divide by zero. Might just be in the signal handler on checking which signal happened or the like.  Hope that helps.

                  • 6. Re: Eclipse - how to enable exceptions (error handling) ?
                    romankubanek

                    Hi Kurt,

                    Your recomendation with signals works good, but there is problem that I dont know how to continue program without termination (exit(1)...), if this signal is passed to handler.

                    Ok my problem is with this situation: I have tcp client and in case if I call socket function send or write and the socket lost connection to server, then program terminates imediatelly without any information. This is overkill... So I still looking for solution how to handle this critical situation.

                    Thanks

                     

                    Roman

                    • 7. Re: Eclipse - how to enable exceptions (error handling) ?
                      KurtE

                      i think you would need to experiment.  The test app in the link I mentioned, the first exception was handled by the catch statement, by design in that app, the 2nd one aborts.