try: finally: question

Jeff Shannon jeff at ccvcorp.com
Fri Jul 16 14:15:17 EDT 2004


Joshua Marshall wrote:

> Why is this not an argument for disallowing multiple except blocks, like:
>
>  try:
>     raise A()
>  except A:
>     raise B()
>  except B:
>     print 'Did we get here?'
>
>This is legal and well-defined in Python.  If try/except/finally was
>removed it seems like multiple except blocks should have been removed
>too--the behavior of one is as obvious as the other.
>  
>

I'd like to note that, in this case, exactly one of the except blocks 
gets executed.  If an exception is raised within one of the branches, it 
doesn't get caught by later branches.

In the 'finally' case, it's unclear whether it should catch such a 
secondary exception or not.  On the one hand, we have the established 
precedent that an exception within an exception handler goes up to the 
caller.  On the other hand, we have the expectation that 'finally' 
should always be executed when leaving the specified code block.  But 
which block is being specified -- just the 'try' section, or all of the 
'except'  clauses as well?  Syntactically, it seems that try-clause-only 
should be the correct answer, but including all of the except clauses 
would be the most common desire... 

Given the different semantics of finally vs except, I can certainly see 
a *lot* of room for confusion.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list