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

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



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.

Mixing traps with the exception functions in this ways is bound to be hairy
and I'd suggest you avoid doing it.

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com



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