2009/10/15 Steven D'Aprano <st...@remove-this-cybersource.com.au>: > Setting up a try...except block is cheap in Python. According to my > tests, the overhead is little more than that of a single pass statement. > > But actually raising and catching the exception is not cheap. If you use > a lot of exceptions for flow control, performance will probably suffer. > > In C++, exceptions are expensive, whether you catch one or not. > > Also, using exceptions this way is a structured form of GOTO -- it's easy > to abuse and turn it into spaghetti code. Actually, not that easy to > abuse, because you can't jump back into the try block. It's more like a > multi-level break outside of a loop than a general GOTO.
I don't think it's *only* the performance thing, it's also clarity. The understood meaning of throwing an exception is to say "something happened that shouldn't have". If one uses it when something has happened that *should* have, because it happens to have the right behaviour (even if the overhead doesn't matter), then one is misrepresenting the program logic. -- Tim Rowe -- http://mail.python.org/mailman/listinfo/python-list