From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3391 invoked from network); 29 Sep 2005 20:07:34 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 29 Sep 2005 20:07:34 -0000 Received: (qmail 88103 invoked from network); 29 Sep 2005 20:07:28 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 29 Sep 2005 20:07:28 -0000 Received: (qmail 27862 invoked by alias); 29 Sep 2005 20:07:21 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9462 Received: (qmail 27852 invoked from network); 29 Sep 2005 20:07:20 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 29 Sep 2005 20:07:20 -0000 Received: (qmail 87042 invoked from network); 29 Sep 2005 20:07:20 -0000 Received: from ns9.hostinglmi.net (213.194.149.146) by a.mx.sunsite.dk with SMTP; 29 Sep 2005 20:07:19 -0000 Received: from 212.red-80-35-44.staticip.rima-tde.net ([80.35.44.212] helo=localhost) by ns9.hostinglmi.net with esmtpa (Exim 4.52) id 1EL4gc-0001Fo-QC for zsh-users@sunsite.dk; Thu, 29 Sep 2005 22:07:23 +0200 Date: Thu, 29 Sep 2005 22:07:41 +0200 From: DervishD To: Zsh Users Subject: Exception handling and "trap" vs. TRAPNAL() Message-ID: <20050929200741.GA1156@DervishD> Mail-Followup-To: Zsh Users Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.4.2.1i Organization: DervishD X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - ns9.hostinglmi.net X-AntiAbuse: Original Domain - sunsite.dk X-AntiAbuse: Originator/Caller UID/GID - [0 0] / [47 12] X-AntiAbuse: Sender Address Domain - dervishd.net X-Source: X-Source-Args: X-Source-Dir: X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 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...