zsh-users
 help / color / mirror / code / Atom feed
* About zsh-users 8489 (exception handling)
@ 2005-09-27 23:47 DervishD
  2005-09-28  2:42 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: DervishD @ 2005-09-27 23:47 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

    In zsh-users 8489 Bart Schaeffer proposes using this syntax:

    { ${:?THROW} } 2>/dev/null

    instead of this one:

    { ${*ERROR*} } 2>/dev/null

    for generating the exception in "throw" function, since the
latter may become valid syntax in the future.

    Unfortunately, that doesn't work because ${:?...} *exits* from
the shell, so the valid syntax would be:

    ( ${:?THROW} ) 2>/dev/null

    For me, using a subshell just to generate the exception seems a
bit excessive. 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?

    Thanks a lot :)

    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...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: About zsh-users 8489 (exception handling)
  2005-09-27 23:47 About zsh-users 8489 (exception handling) DervishD
@ 2005-09-28  2:42 ` Bart Schaefer
  2005-09-28  8:43   ` DervishD
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2005-09-28  2:42 UTC (permalink / raw)
  To: Zsh Users

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
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: About zsh-users 8489 (exception handling)
  2005-09-28  2:42 ` Bart Schaefer
@ 2005-09-28  8:43   ` DervishD
  2005-09-28 15:18     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: DervishD @ 2005-09-28  8:43 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

    You came to the rescue, as always ;)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> 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 ...

    But I use (or will use) throw mainly in scripts, and that makes
the script to exit. And probably the 90% of throw/catch usage will
take place in scripts, not in interactive shells...
 
> : 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.

    That's VERY clever, dude :))) I like this solution a lot :)

> I think EXCEPTION should also be declared with "typeset -g", so that
> makes the complete function look like:

    But the function will work correctly even without "typeset -g",
wouldn't it? Wouldn't be needed "typeset -g" in "catch" too for the
CAUGHT parameter?

    Thanks a lot for your answer :)

    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...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: About zsh-users 8489 (exception handling)
  2005-09-28  8:43   ` DervishD
@ 2005-09-28 15:18     ` Bart Schaefer
  2005-09-29 10:31       ` DervishD
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2005-09-28 15:18 UTC (permalink / raw)
  To: Zsh Users

On Sep 28, 10:43am, DervishD wrote:
}
} > I think EXCEPTION should also be declared with "typeset -g"
} 
}     But the function will work correctly even without "typeset -g",
} wouldn't it?

Yes.  It's just making it obvious that a "local" was not forgotten.

} Wouldn't be needed "typeset -g" in "catch" too for the
} CAUGHT parameter?

I'd probably put one there, yes.  I hadn't looked again at "catch".


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: About zsh-users 8489 (exception handling)
  2005-09-28 15:18     ` Bart Schaefer
@ 2005-09-29 10:31       ` DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2005-09-29 10:31 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> On Sep 28, 10:43am, DervishD wrote:
> } > I think EXCEPTION should also be declared with "typeset -g"
> } 
> }     But the function will work correctly even without "typeset -g",
> } wouldn't it?
> Yes.  It's just making it obvious that a "local" was not forgotten.

    Ok, I see then :)

    Thanks a lot again :)

    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...


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-09-29 10:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-27 23:47 About zsh-users 8489 (exception handling) DervishD
2005-09-28  2:42 ` Bart Schaefer
2005-09-28  8:43   ` DervishD
2005-09-28 15:18     ` Bart Schaefer
2005-09-29 10:31       ` DervishD

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).