[Python-ideas] try-else without except or finally
Rob Cliffe
rob.cliffe at btinternet.com
Thu Nov 10 15:31:04 CET 2011
Bear in mind new and inexperienced users, or perhaps someone who doesn't
know Python at all but is trying to figure out what a Python program does.
try:
...
else:
...
is not at all intuitive.
If we want to allow that syntax, it would be better if 'else' were
replaced by something more meaningful like 'ifnoexception'. Maybe
someone can think of something meaningful but more concise.
On 10/11/2011 14:24, Steven D'Aprano wrote:
> 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).
>
>
More information about the Python-ideas
mailing list