2012/4/5 Stas Malyshev <smalyshev@sugarcrm.com>
it's a wonderful mechanism with more uses than simply reporting errors
- the aspect of transferring control is what I find really interesting
about exceptions.
Exceptions should not be used for flow control. [...]

While exceptions themselves may not be suitable for a general-purpose
control structure, they do embody one. It's an early-return mechanism that
you also see in some of the other control structures mentioned in this
thread (Python's generators, Common LISP's conditions). Even the "return"
statement itself could be considered a simpler form of it, with the
difference that "return" can only go up a single invocation while "throw"
can exit an arbitrary number of them (also, "throw" is limited in the type
of value it can return).

The argument against any early-return mechanism is that it creates a
too-complex control flow. This is the same argument from the structured
programming wars about having multiple exit
points<http://c2.com/cgi/wiki?SingleFunctionExitPoint> from
a function, in support of having a single return at the end of functions.
It certainly can produce messy, hard to understand code, but it can also be
quite powerful, simplifying certain problems. Perhaps there's some middle
ground that balances clarity, simplicity and power.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 19 of 23 | next ›
Discussion Overview
groupphp-internals @
postedApr 2, '12 at 12:44p
activeApr 6, '12 at 1:59p



site design / logo © 2018 Grokbase