[Tutor] Re: converting string to a number
Jeff Shannon
jeff at ccvcorp.com
Thu Sep 2 21:27:07 CEST 2004
nik wrote:
> Thanks for the advice guys, I hadn't thought of what would happen if it
> wasn't the exception you were expecting. It still feels uncomfortable
> though, I've always felt an exception is *exceptional*, ie something
> that was outside your control.
The feeling you have is consistent with how *most* languages use
exceptions, so it's entirely understandable. However, Python
exceptions are less costly than many other languages' exceptions, so
it's a fairly standard control-flow mechanism in Python.
Indeed, you may not realize it, but even basic control structures are
built on top of exceptions. As an example, for and while loops use a
StopIteration exception to indicate that they are done. The handling
for this is automatically created in the bytecode and is thus hidden
from the normal user, but if you peek under the covers, it's there.
So yes, this is a perfectly acceptable usage of exceptions -- Python
tends to follow the old truism that "It's easier to ask for
forgiveness than for permission" -- in most cases, it's more
straightforward to try an action and catch the exception if it doesn't
work, than it is to check ahead of time whether the action will work.
Jeff Shannon
Technician/Programmer
Credit International
More information about the Tutor
mailing list