On Fri, Aug 23, 2013 at 10:30 AM, Alan Gauld <alan.ga...@btinternet.com> wrote:
> On 22/08/13 21:27, Chris Down wrote:
>
>> You can also use the "else" clause if there is stuff you want to run if
>> the try
>> block doesn't raise the caught exception, which avoids putting it in "try"
>> if
>> you don't intend to exit from the exception.
>
>
> I admit that I've never really found a use for else in a try block.
> I don;t see much advantage in
>
> try: f(x)
> except MyError:
>     pass
> else:
>     g(x)
> h(x)
>
> over
>
> try: f(x)
> except MyError:
>     pass
> g(x)
> h(x)
>
> Unless you really only want g(x) executed if there
> is no MyError exception but want h(x) executed regardless.
>
> I   guess where h() is not using x it might be helpful but in most(all?) of
> my code I've usually bailed when x has gone
> wrong or I've fixed things such that hg() and h() are required.
>
> I'm curious, how often do others use the try/else combination?

I think one use of writing something in an "else" would be to write
those statements which are part of a "block" (loosely used) of
statements which would depend on the statement in the try block. That
doesn't change the way it works if they were not written in an "else",
but perhaps makes it clear that those statements are to be executed
only when there is no exception. Explicit is better than implicit, may
be?

-- 
http://echorand.me
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to