From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@sunsite.dk>
Subject: Re: About zsh-users 8489 (exception handling)
Date: Wed, 28 Sep 2005 02:42:49 +0000 [thread overview]
Message-ID: <1050928024249.ZM23758@candle.brasslantern.com> (raw)
In-Reply-To: <20050927234726.GC988@DervishD>
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
}
next prev parent reply other threads:[~2005-09-28 2:43 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-27 23:47 DervishD
2005-09-28 2:42 ` Bart Schaefer [this message]
2005-09-28 8:43 ` DervishD
2005-09-28 15:18 ` Bart Schaefer
2005-09-29 10:31 ` DervishD
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1050928024249.ZM23758@candle.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=zsh-users@sunsite.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).