[Python-ideas] try-else without except or finally

Steven D'Aprano steve at pearwood.info
Thu Nov 10 15:24:57 CET 2011


Rob Cliffe wrote:

> "except: raise" makes it explicit that an exception is to be 
> propogated.  Far from being pointless it makes the code much more 
> readable.  "Explicit is better than implicit."

Look, I love the Zen as much as the next guy, but sometimes they get 
used as thought-terminating cliches. Catching an exception only to 
unconditionally raise it again unchanged is a waste of everybody's time: 
don't catch exceptions you have no intention of dealing with.

Python has allowed try...finally without an except clause since version 
1.5, if not earlier.

Virtually every line of Python code may implicitly raise an exception. 
Despite the Zen, we don't go around writing

try:
     n = len(x)
except:
     raise

just to satisfy the urge to make it explicit that any exception that 
occurs will be propagated. We don't need an explicit reminder that any 
exceptions will be propagated because, absent an except clause to 
explicitly silence it, that's what exceptions do: they propagate.

Errors should never pass silently.
Unless explicitly silenced.

(see, I can quote the Zen too).


-- 
Steven



More information about the Python-ideas mailing list