zsh-workers
 help / color / mirror / code / Atom feed
* Exception handling and "trap" vs. TRAPNAL()
@ 2005-10-01  8:01 DervishD
  0 siblings, 0 replies; 25+ messages in thread
From: DervishD @ 2005-10-01  8:01 UTC (permalink / raw)
  To: Zsh Workers

    Hi all :)

    I've tried twice to forward this message from zsh-users to
zsh-workers, with no success. Has this list any kind of limitation
for forwarded material? Well, anyway, here is the message again.

    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. I'm not sure if this is an intended
behaviour or a zsh bug, since there have been bugs related to traps
when saving/restoring shell state in the past, and this may be one of
those bugs.

    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...


^ permalink raw reply	[flat|nested] 25+ messages in thread
[parent not found: <20050929200741.GA1156@DervishD>]
* Exception handling and "trap" vs. TRAPNAL()
@ 2005-10-01  7:45 DervishD
  0 siblings, 0 replies; 25+ messages in thread
From: DervishD @ 2005-10-01  7:45 UTC (permalink / raw)
  To: Zsh Workers

    Hi all :))

    I forwarded this message yesterday, but it hasn't made its way
into the list, don't know why :? so I'm posting it here again.

    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...


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2005-10-04 17:45 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-01  8:01 Exception handling and "trap" vs. TRAPNAL() DervishD
     [not found] <20050929200741.GA1156@DervishD>
     [not found] ` <20050930124130.45eb0463.pws@csr.com>
     [not found]   ` <20051001153756.GA12183@DervishD>
2005-10-01 18:38     ` Bart Schaefer
2005-10-01 19:10       ` Peter Stephenson
2005-10-01 20:41         ` DervishD
2005-10-01 22:44         ` Bart Schaefer
2005-10-02  8:06           ` DervishD
2005-10-01 20:28       ` DervishD
2005-10-02  4:40         ` Bart Schaefer
2005-10-02  8:13           ` DervishD
2005-10-02 19:09           ` Peter Stephenson
2005-10-02 19:55             ` Bart Schaefer
2005-10-02 23:00               ` DervishD
2005-10-03  1:37                 ` Bart Schaefer
2005-10-03  8:57                   ` Peter Stephenson
2005-10-03 14:51                     ` Bart Schaefer
2005-10-03 15:10                       ` Peter Stephenson
2005-10-03 16:50                         ` Bart Schaefer
2005-10-03  9:01                   ` DervishD
2005-10-03 16:21                     ` Bart Schaefer
2005-10-03 17:59                       ` DervishD
2005-10-04 16:31                         ` Bart Schaefer
2005-10-04 17:29                           ` DervishD
2005-10-04 17:34                             ` Peter Stephenson
2005-10-04 17:46                               ` DervishD
  -- strict thread matches above, loose matches on Subject: below --
2005-10-01  7:45 DervishD

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).