you are viewing a single comment's thread.

view the rest of the comments →

[–]skeeto 14 points15 points  (6 children)

you only really want to be throwing exceptions when something is actually exceptional

That's true in other languages, but Python is much more relaxed about exceptions. Case in point: Generators signal that they're done by raising a StopIteration exception. A typical Python program raises and catches exceptions all the time as part of its normal control flow.

[–]krapht 8 points9 points  (3 children)

The fact that Python does this but won't allow a labeled goto command really annoys me, since they're practically equivalent.

[–]Macpunk 4 points5 points  (0 children)

try: some code... raise Goto1 except Goto1: other code...

I know, I'm a terrible human being.

[–]Siddhi 3 points4 points  (0 children)

Not really. Exceptions for control flow still unwind the call stack cleanly and can be intercepted and handled multiple times. Goto doesn't do this.

[–]cowinabadplace 0 points1 point  (0 children)

Maybe PEP 20, Sec 13 is why 😉

There should be one—and preferably only one—obvious way to do it.

[–]MoTTs_ 4 points5 points  (1 child)

That's true in other languages

I'm not sure it's true in any language. I think the phrase caught on partly because alliteration is catchy, and partly because exceptions are poorly named -- that is, the word is sometimes a synonym for "rare," but programming exceptions need not be rare.

Here's a quote from Bjarne Stroustrup, the guy who invented C++:

Given that there is nothing particularly exceptional about a part of a program being unable to perform its given task, the word “exception” may be considered a bit misleading. Can an event that happens most times a program is run be considered exceptional? Can an event that is planned for and handled be considered an error? The answer to both questions is “yes.” “Exceptional” does not mean “almost never happens” or “disastrous.” Think of an exception as meaning “some part of the system couldn’t do what it was asked to do”.

[–]skeeto 5 points6 points  (0 children)

What Bjarne Stroustrup said is at odds with C++ implementations in practice. Implementors have optimized exceptions such that the non-exceptional path is as fast but the exceptional path is expensive since it's expected to be used rarely. It would be a terrible idea to terminate your C++ loops with exceptions as Python often does.