Bernd wrote 11 lines
2011/3/9 Hans-Peter Diettrich :
> An exception down in some calculation doesn't give you any clue of what
> exactly went wrong, only that *something* went wrong.
It can often be useful and remove a lot of if/else for cleanly exiting
a bunch of nested loops and functions (and also save clock cycles and
make the code much more compact and clearer) if you let it
*automatically* unwind the the call stack (instead of writing code for
this) whenever the exception occurs (of course only if you really know
why your exceptions would occur).
That's usually when exceptions are raised explicitely. Either by you or by some deeper library part. Not during calculation.
Many people recommend not using exceptions as control structures but
IMHO exceptions *are* control structures and can (and should) be used
as such (if appropriate).
A big if... And it depends on the application. Specially applications that have abortable transactions (web and db) this is useful, since one exception and a few try finallys can safely abort the current transaction, and return to a known good state. (waiting for next request)
In many other applications, the program is essentially in an undefined state, and continuing is risky or useless.
In some other languages (Python for example)
this is even considered good programming style and recommended because
it can make certain code much cleaner and easier to understand.
Python also considers block-by-ident good practice :-)