From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18832 invoked from network); 1 Oct 2005 15:37:50 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 1 Oct 2005 15:37:50 -0000 Received: (qmail 99437 invoked from network); 1 Oct 2005 15:37:43 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 1 Oct 2005 15:37:43 -0000 Received: (qmail 28910 invoked by alias); 1 Oct 2005 15:37:33 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9464 Received: (qmail 28900 invoked from network); 1 Oct 2005 15:37:33 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 1 Oct 2005 15:37:33 -0000 Received: (qmail 98048 invoked from network); 1 Oct 2005 15:37:33 -0000 Received: from ns9.hostinglmi.net (213.194.149.146) by a.mx.sunsite.dk with SMTP; 1 Oct 2005 15:37:31 -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 1ELjQc-0007Y9-Qg; Sat, 01 Oct 2005 17:37:35 +0200 Date: Sat, 1 Oct 2005 17:37:56 +0200 From: DervishD To: Peter Stephenson Cc: zsh-users@sunsite.dk Subject: Re: Exception handling and "trap" vs. TRAPNAL() Message-ID: <20051001153756.GA12183@DervishD> Mail-Followup-To: Peter Stephenson , zsh-users@sunsite.dk References: <20050929200741.GA1156@DervishD> <20050930124130.45eb0463.pws@csr.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20050930124130.45eb0463.pws@csr.com> 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 Peter :) * Peter Stephenson dixit: > DervishD 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...