Page 1 of 1

Disarming local exception handlers

Posted: Mon Sep 07, 2009 1:22 pm
by Simon B
Hi all.

Recently we had an issue where a local exception handler armed in method A (exHandlerA) was being disarmed by another method. So, method A calls B calls C calls D. D arms its own local exception handler (exHandlerD) inside an if clause, then has "on Exception do null;" outside the if clause. Which actually disarms exHandlerA (when the if clause is not executed) - as that is the next local handler on the exception handler stack.

First thing we knew a problem had occurred was we started getting stack dumps as exHandlerA wasn't armed (because of the funky code in method D).

Which makes me think it would be good if JADE had an option like this:

on Exception do null errorIfNotArmedInThisMethod;

alternatively, an ini file option to do the same. Does anyone think disarming a local exception handler in a method other than the one that armed it is useful / acceptable coding style ?

What do you think ?

Cheers, Simon B. (long time lurker).

Re: Disarming local exception handlers

Posted: Mon Sep 07, 2009 10:21 pm
by bdoolan
Hi Simon,
"Funky code" sounds like putting it mildly.

I would say that disarming a local exception handler (using "on exception do null") in a method other than the one it was armed in should be avoided if at all possible. There may be very rare cases where it makes sense but I've never come across any. I avoid "on exception do null" and don't actually use it at all. I just let returning from the method disarm the handler automatically. If it is ever done, there should be some comments explaining why it was necessary.

The problem is that "on exception do null" is not specifically targetted - it just disarms the handler on the top of the stack and that may change as code is enhanced. Therefore, it could easily happen that everything worked fine when the code was first written but a new handler was then added which breaks the code by disarming the wrong handler.

I often set up local handlers with a parameter "isArmed : Boolean" in the handler method signature and having the code in the exception handler method look like

if isArmed then
// handle exception
else
return Ex_Pass_Back;
endif;

Then I can easily arm and disarm the local handler with minimal overhead and in a targetted and finely controlled fashion. Given this coding style, I've never needed to use "on exception do null".

Perhaps your new ini file option just tells the compiler to error "on exception do null" rather than make it a runtime check (still allow "on exception do null global" though). On balance, probably too radical. Just don't use it!

Cheers, Brendan

Re: Disarming local exception handlers

Posted: Tue Sep 08, 2009 11:42 am
by Simon B
Hi Brendan.

Yeah - my feeling is an "on Exception do null;" in a method that doesn't unconditionally arm the local exception handler is a bug - or very bad coding style.

I like your isArmed boolean. Nice way around the issue we struck. Of course, if we're talking buggy code someone could still forget to set isArmed true/false correctly, but at least that bug won't unintentionally disarm out of method exception handlers.

Cheers, Simon.