else: w/o except: - why not?

Tim Peters tim.one at comcast.net
Mon Mar 31 19:21:18 EST 2003


[Manus Hand]
> I know that if you want to use else: on a try: block, you need to
> specify one or more except: clauses.  I guess my question is why
> this should need to be.
>
> Personally, I have cases where it would be nice to do this:
>
> try:
>     # some code that may except
> else:
>     # but if it didn't except, I want to do this

Sorry, I have no idea what you want that to do if an exception is raised in
"# some code".

> Instead of writing the code that way, I need to write this:
>
> try:
>     # some code that may except
> except:
>     # and if it does, then, okay, just ignore it
>     pass
> else:
>     # but if it didn't except, I want to do this

Now I know what you intended, assuming you wrote what you meant.  Reason
enough for me.  If you want the absence of an except clause to imply the
presence of a catch-everything except clause, there's really no chance:  a
bare except is bad practice.  For example, it catches SystemExit and
KeyboardInterrupt too, and bare excepts rarely *intend* to catch those.
Guido isn't going to make it easier to write poor code.






More information about the Python-list mailing list