zsh-users
 help / color / mirror / code / Atom feed
From: DervishD <zsh@dervishd.net>
To: Peter Stephenson <pws@csr.com>
Cc: zsh-users@sunsite.dk
Subject: Re: Exception handling and "trap" vs. TRAPNAL()
Date: Sat, 1 Oct 2005 17:37:56 +0200	[thread overview]
Message-ID: <20051001153756.GA12183@DervishD> (raw)
In-Reply-To: <20050930124130.45eb0463.pws@csr.com>

    Hi Peter :)
 * Peter Stephenson <pws@csr.com> dixit:
> DervishD <zsh@dervishd.net> 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...


      reply	other threads:[~2005-10-01 15:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-29 20:07 DervishD
2005-09-30 11:41 ` Peter Stephenson
2005-10-01 15:37   ` DervishD [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051001153756.GA12183@DervishD \
    --to=zsh@dervishd.net \
    --cc=pws@csr.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).