Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Exception handling and "trap" vs. TRAPNAL()

    Hi Peter :)
 * Peter Stephenson <pws@xxxxxxx> dixit:
> DervishD <zsh@xxxxxxxxxxxx> wrote:
> > --- cut here ---
> > #!/bin/zsh
> > 
> > emulate -L zsh
> > 
> > [[ "$1" = "trap" ]] && trap 'throw DEFAULT' ZERR
> > [[ "$1" = "TRAPZERR" ]] && function TRAPZERR() { throw DEFAULT ; }
> > 
> > {
> >     print "Before throwing"
> >     # This should throw "DEFAULT" exception
> >     false
> >     throw EXCEPTION
> >     # This shouldn't be shown
> >     print "After throwing"
> > } always {
> >     catch * && print "Caught exception $CAUGHT"
> >     return 0
> > }
> > 
> > --- cut here ---
> >     $ ./script trap
> >     Before throwing
> >     Caught exception EXCEPTION
> >
> >     I'm puzzled. The "trap" trap is executed in the current
> > environment, so I assume it would throw "DEFAULT", as intended, as
> > soon as we hit the "false". It doesn't and I don't know why. OTOH,
> > the TRAPZERR function, which runs "throw" in its own environment,
> > works ok :??? WHY?
> Add some "print"s to the trap to see when it's triggering.  You may find it's
> throwing additional exceptions at points you don't want it to which is
> gumming things up.

    The trap is triggering just in the "false" statement, and the
TRAPZERR works perfectly, it's the "trap" kind which is failing.
> Mixing traps with the exception functions in this ways is bound to be hairy
> and I'd suggest you avoid doing it.

    But the TRAPZERR works seamlessly :? In fact, I need to use
"trap" instead because I need to handle line numbers and the like,
otherwise I would stick to TRAPZERR. I can undertand that throwing
exceptions from within a trap can be hairy, but since I tested
TRAPZERR and it worked, I assumed that the system worked. I can avoid
using ZERR, of course, but that implies less clean code in some
scripts I have. I would have to substitute every command with
"command && throw $LINENO" or something similar (or worse, I would
have to think about a different name for the exceptions I need...) to
handle all errors in a common place. I don't even need exceptions for
this, but using exceptions and traps leads to much simpler, cleaner
and easier to maintain code.

    So: assuming that the trap is being triggered correctly (it is,
I've tested it), why "trap" is failing while "TRAPZERR" is working?
Can I do anything to make it work (except patching zsh)? Is there any
other way of throwing exceptions automagically when a command returns
a non-zero status or must I go for explicit code instead of making
the system implicit using ZERR?.

    Thanks a lot for your answer, Peter :) I was sure I was making
some obvious mistake O:)

    Raúl Núñez de Arenas Coronado

Linux Registered User 88736 | http://www.dervishd.net
http://www.pleyades.net & http://www.gotesdelluna.net
It's my PC and I'll cry if I want to...

Messages sorted by: Reverse Date, Date, Thread, Author