FAQ

[PHP-INTERNALS] resume after exception

Galen Wright-Watson
Apr 5, 2012 at 11:35 pm
2012/4/5 Stas Malyshev <smalyshev@sugarcrm.com>
Hi!
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.
reply

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions