try:else: w/o except: - why not?

yvan yvan_charpentier at hotmail.com
Mon Mar 31 23:49:51 EST 2003


manus at python.net (Manus Hand) wrote in message news:<1c004553.0303311553.53a9a92b at posting.google.com>...
> 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
> 
> 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
> 
> I know it's a core language decision and therefore up to Guido,
> but I am curious (Guido?) if there is something I am missing that
> would make a simple try:else: construction (without requiring any
> except: blocks) not make sense?
> 
> Manus

Unless your code throws, execution will resume, so the following
should behaves as you want.

try:
     # some code that may throws
     # Some code that will be executed if it did not throw above, will
not if it threw (this is what is in your else block)
except:
     # will get there if it threw, will not otherwise




More information about the Python-list mailing list