Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Exception handling and "trap" vs. TRAPNAL()
- X-seq: zsh-workers 21787
- From: DervishD <zsh@xxxxxxxxxxxx>
- To: Zsh Workers <zsh-workers@xxxxxxxxxx>
- Subject: Exception handling and "trap" vs. TRAPNAL()
- Date: Sat, 1 Oct 2005 09:45:12 +0200
- Mail-followup-to: Zsh Workers <zsh-workers@xxxxxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxxxxx; run by ezmlm
- Organization: DervishD
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...
Messages sorted by:
Reverse Date,
Date,
Thread,
Author