Main Site Documentation

Trying to catch all exceptions


#1

I’m trying to implement exception handling in my program. I’ve done the simple thing of putting try/catch blocks around the obvious places and they work fine. Now I’m trying to put a try/catch block around the whole program to catch exceptions anywhere. Since the vast majority of my program runs in a while loop, it isn’t all that hard. Here’s a pseudo code version of what I’m doing.


            missionRun = true;
            while (missionRun)
            {
                try
                {
                    callThisMethod();
                    callThatMethod();
                    callTheOtherMethod();

                    CPFUtilities.divideByZero;
                }
                catch (Exception currentException)
                {
                    sbTemp.Clear();
                    sbTemp.Append("*** Exception in State Machine loop: ");
                    sbTemp.Append(currentException.HResultToString());
                    EngrLogger.writeComment(UTF8Encoding.UTF8.GetBytes(EngrLogger.getPrefix(sbTemp).ToString()));

                    CPFStateTimer.Change(configFile.EATimeout, configFile.timeoutDefaultPeriod);
                    CPFState = CPFStates.EmergencyAscend;
                }
            }

The .divideByZero method is the test method I use to force an exception. I can put the .divideByZero where shown and the catch block works fine, I can put it in one of the methods called in the main loop and the catch block works fine, I can put it in a method that is called by a method in the main loop and the catch block works fines. However, if I put the .divideByZero method in an event handler, an exception is thrown that isn’t caught by my catch block and my program hangs.

Can anyone explain why my catch block seems to catch any exception that pops up in a method but not an exception that pops up in an event handler? More importantly, is there a better solution than putting a try/catch block around every event handler in my program?

Thanks


#2

@ Gene - Interrupt event handlers run in seperate system thread. As Andre said, you will need to catch exceptions within the event handlers.


#3

It is a good idea in general to have a try catch block in every event handler. You never know what thread it is called from. You also don’t know if the caller of the event handles exceptions correctly. My be it does not clean up some stuff, may be your app will crash.
Also, in every thread start method should be a try catch block.
Because any uncaught exception in any thread will crash the complete app.
In best case the device will reboot then, but it might also freeze.


#4

please vote :slight_smile:

For now you can only use Watchdog to reset the device in case of a freeze…

Jay.


#5

Well the good news is this forum (and the community) is an incredibly effective way to get answers to difficult (at least for me) questions. The bad news is the answer isn’t always what I want to hear.

Oh well, lots of typing in my future.

Thanks for the help


#6

You are welcome !

just make sure you vote on the above, the more votes we get the more chances it will get added in future releases… so I recommend anyone reading this to vote…

cheers,
Jay,


#7

exception handling takes a toll on performance in general, so having it everywhere is not really a recommended approach, also a global handler will help debug code and handle exceptions that happen outside of your code… think drivers, libraries so on… and not to forget you can generate bug reports from within your device even after it’s deployed…

Cheers,


#8

Since we’re on the subject, does anyone know how to interpret the StackTrace property of the Exception class?