zsh-workers
 help / color / mirror / code / Atom feed
From: DervishD <zsh@dervishd.net>
To: Zsh Workers <zsh-workers@sunsite.dk>
Subject: Fwd: [Exception handling and "trap" vs. TRAPNAL()]
Date: Fri, 30 Sep 2005 20:44:03 +0200	[thread overview]
Message-ID: <20050930184403.GA617@DervishD> (raw)

    Hi all :)

    I'm forwarding this message here after looking at the zsh-workers
mail archive. There have been problems with "trap" in the past,
specially when saving/restoring state, and this problem is maybe
caused by this (or my stupidity, who knows...).

    Thanks a lot in advance :)

----- Forwarded message from DervishD <zsh@dervishd.net> -----

> Date: Thu, 29 Sep 2005 22:07:41 +0200
> From: DervishD <zsh@dervishd.net>
> To: Zsh Users <zsh-users@sunsite.dk>
> Subject: Exception handling and "trap" vs. TRAPNAL()
> 
>     Hi all :)
> 
>     Let's say we have this script, using exception handling:
> 
> --- cut here ---
> #!/bin/zsh
> 
> emulate -L zsh
> 
> [[ "$1" = "trap" ]] && trap 'throw DEFAULT' ZERR
> [[ "$1" = "TRAPZERR" ]] && function TRAPZERR() { throw DEFAULT ; }
> 
> function throw() {
>   typeset -g EXCEPTION="$1"
>   readonly THROW=0
>   if (( TRY_BLOCK_ERROR == 0 )); then
>     (( TRY_BLOCK_ERROR = 1 ))
>   fi
>   THROW= 2>/dev/null
> }
> 
> function catch() {
>     typeset -g CAUGHT
>     if [[ $TRY_BLOCK_ERROR -gt 0 && $EXCEPTION = ${~1} ]]; then
>         (( TRY_BLOCK_ERROR = 0 ))
>         CAUGHT="$EXCEPTION"
>         unset EXCEPTION
>         return 0
>     fi
>     return 1
> }
> 
> alias catch="noglob catch"
> 
> 
> {
>     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 ---
> 
>     Sorry for the size, but I want to make sure it is as
> self-contained as possible. Well, I do the following:
> 
>     $ ./script trap
>     Before throwing
>     Caught exception EXCEPTION
> 
>     $ ./script TRAPZERR
>     Before throwing
>     Caught exception DEFAULT
> 
>     $ ./script
>     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?
> 
>     AFAIK the "trap" builtin runs its arguments in the current
> environment, so to say it's like an alias, like cutting and pasting
> the code, but anyway that shouldn't be the problem, since "throw" is
> using globals and so it affects running in any environment.
> 
>     The only thing that could be happening is that the ZERR trap
> using "trap" is not setting "TRY_BLOCK_ERROR" to "1", or it sets the
> variable but the variable gets reset upon exiting the trap :???
> 
>     Can anybody (and I really mean anybody, not Bart XD) explain this
> to me? I would like to use something similar and I need to use "trap"
> traps and not "TRAPNAL()" traps because I want to use LINENO and
> probably a couple of variables which are local to the functions where
> the ZERR trap can be raised.
> 
>     Thanks a lot in advance :)
> 
>     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...
> 
> 

----- End forwarded message -----

    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 12:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-30 18:44 DervishD [this message]
2005-10-01 16:22 ` Bart Schaefer
2005-10-01 17:24   ` DervishD

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=20050930184403.GA617@DervishD \
    --to=zsh@dervishd.net \
    --cc=zsh-workers@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).