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

Rob Cliffe rob.cliffe at btinternet.com
Thu Nov 10 14:35:57 CET 2011



On 10/11/2011 13:28, Steven D'Aprano wrote:
> Yuval Greenfield wrote:
>
>> "Try...else..." doesn't make any sense without the except jn my eyes. 
>> The
>> "else" has to conjugate something.
>
>
> We already have while...else and for...else blocks in Python. The 
> principle is the same:
>
>
> while condition():
>     ...
> else:
>     # Run the else block unless we exit the while block with a break.
>     ...
>
>
> try:
>     ...
> else:
>     # Run the else block unless we exit the try block with an exception.
>     ...
> finally:
>     ...
>
>
> This currently doesn't work without a pointless "except: raise" clause 
> to satisfy the compiler. I try to avoid writing pointless code.
>
>
>
"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."
Rob Cliffe



More information about the Python-ideas mailing list