From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28077 invoked from network); 28 Sep 2005 02:43:14 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 28 Sep 2005 02:43:14 -0000 Received: (qmail 45715 invoked from network); 28 Sep 2005 02:43:08 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 28 Sep 2005 02:43:08 -0000 Received: (qmail 9936 invoked by alias); 28 Sep 2005 02:42:59 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9452 Received: (qmail 9927 invoked from network); 28 Sep 2005 02:42:59 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 28 Sep 2005 02:42:59 -0000 Received: (qmail 44596 invoked from network); 28 Sep 2005 02:42:59 -0000 Received: from vms040pub.verizon.net (206.46.252.40) by a.mx.sunsite.dk with SMTP; 28 Sep 2005 02:42:58 -0000 Received: from candle.brasslantern.com ([71.116.81.225]) by vms040.mailsrvcs.net (Sun Java System Messaging Server 6.2 HotFix 0.04 (built Dec 24 2004)) with ESMTPA id <0INI00AAUA7H4P73@vms040.mailsrvcs.net> for zsh-users@sunsite.dk; Tue, 27 Sep 2005 21:42:56 -0500 (CDT) Received: from candle.brasslantern.com (IDENT:schaefer@localhost [127.0.0.1]) by candle.brasslantern.com (8.12.11/8.12.11) with ESMTP id j8S2goar023760 for ; Tue, 27 Sep 2005 19:42:51 -0700 Received: (from schaefer@localhost) by candle.brasslantern.com (8.12.11/8.12.11/Submit) id j8S2gn25023759 for zsh-users@sunsite.dk; Tue, 27 Sep 2005 19:42:50 -0700 Date: Wed, 28 Sep 2005 02:42:49 +0000 From: Bart Schaefer Subject: Re: About zsh-users 8489 (exception handling) In-reply-to: <20050927234726.GC988@DervishD> To: Zsh Users Message-id: <1050928024249.ZM23758@candle.brasslantern.com> MIME-version: 1.0 X-Mailer: Z-Mail (5.0.0 30July97) Content-type: text/plain; charset=us-ascii References: <20050927234726.GC988@DervishD> Comments: In reply to DervishD "About zsh-users 8489 (exception handling)" (Sep 28, 1:47am) 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.5 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 On Sep 28, 1:47am, DervishD wrote: : : In zsh-users 8489 Bart Schaeffer proposes using this syntax: : : { ${:?THROW} } 2>/dev/null : : Unfortunately, that doesn't work because ${:?...} *exits* from : the shell Not in an interactive shell, it doesn't. However ... : Is there any other way of generating an error (the exception) without : using a subshell and without using current invalid syntax that could : become valid in the future? Yes. If you declare (at the top of the throw function) readonly THROW then THROW= 2>/dev/null causes the error, which can be caught with "always". You don't even need the extra layer of { }. I like this a lot, because it uses only valid syntax that is pretty much guaranteed always to remain valid. I think EXCEPTION should also be declared with "typeset -g", so that makes the complete function look like: throw() { typeset -g EXCEPTION="$1" readonly THROW if (( TRY_BLOCK_ERROR == 0 )); then # We are throwing an exception from the middle of an always-block. # We can do this by restoring the error status from the try-block. # (I am not convinced I ever intended this to work, but it does...) (( TRY_BLOCK_ERROR = 1 )) fi # Raise an error, but don't show an error message. # This is a bit of a hack. (Surprised?) THROW= 2>/dev/null }