who is simpler? try/except/else or try/except

greg greg at cosc.canterbury.ac.nz
Sun Aug 12 20:52:08 EDT 2007


Peter Otten wrote:
> try:
>     <operation that may fail>
> except <Error I can handle>:
>    <fix it>
> else:
>     <build on successful operation>
> 
> When you move the else suite into the try...except
> 
> try:
>     <operation that may fail>
>     <build on successful operation> #
> except <Error I can handle>:
>    <fix it>

Note that in general the semantics of these are different,
since in the first version the except clause will only catch
exceptions in <operation that may fail>, whereas in the
second it may catch exceptions in <build on successful operation>
as well.

It probably doesn't make a difference in this example, but
there are situations where it can.

--
Greg



More information about the Python-list mailing list