Re: Stylistic question on control transfer (return vs. throw)
- From: "Kaz Kylheku" <kkylheku@xxxxxxxxx>
- Date: 5 Feb 2006 19:12:54 -0800
Mikalai wrote:
Kenny Tilton wrote:
You found /some/ similarities. You missed some differences.
If I am looking at a throw, I have no idea where it will land, including
a backtrace. If I am looking at (return-from X...) I know the function
or block X will simply exit with the value specified.
Then we have dynamic vs lexical, the runtime cost thereof....
That is it.
Stylistically, one only uses throw in the way one would signal an error.
ie, You want control to jump from here-and-now to whoever-cares-if-anyone.
OK. Where is it clearer to use throw not an error?
Here is one situation. I have used THROW in order to define a protocol
for bailing out of self-contained body of code after all internal error
handling has failed. So the situation was no longer an error, just the
ultimate escape mechanism. The THROW is what a catch-all error hander
would do if it could not find a suitable restart.
The outer wrapper would just exit to the operating system with a
termination status. But I did not want to scatter these exit calls
throughout the code, because unwinding would not take place.
INVOKE-RESTART could have been used also, but THROW and CATCH quite
simply have simpler syntactic sugar.
In Lisp, dynamic non-local returns aren't used for error situations:
conditions are. A dynamic return is done as the last step, when error
handling has been settled.
Normally, this is done with restarts.
A simple catch can be used as a restart when you don't need the ability
of being able to determine whether a restart is available; it's trusted
to just be there.
With restarts, you can enumerate available restarts, determine whether
a particular restart is available, making restarts invisible based on
logical conditions and such.
There are probably overheads in restarts compared to catches for all
these added capabilities.
.
- References:
- Stylistic question on control transfer (return vs. throw)
- From: Mikalai
- Re: Stylistic question on control transfer (return vs. throw)
- From: Kenny Tilton
- Re: Stylistic question on control transfer (return vs. throw)
- From: Mikalai
- Stylistic question on control transfer (return vs. throw)
- Prev by Date: Re: Stylistic question on control transfer (return vs. throw)
- Next by Date: Need help with asdf defsystem
- Previous by thread: Re: Stylistic question on control transfer (return vs. throw)
- Next by thread: Re: Stylistic question on control transfer (return vs. throw)
- Index(es):
Relevant Pages
|