Returning to 'try' block after catching an exception

Ben Finney bignose+hates-spam at benfinney.id.au
Wed May 21 21:12:38 EDT 2008


alex23 <wuwei23 at gmail.com> writes:

> On May 22, 9:13 am, Karlo Lozovina <_karlo_ at _mosor.net_> wrote:
> > In case it's not clear what I meant: after executing
> > some_function() exception SomeExcpetion gets risen. Then, in
> > except block I do something to fix whatever is causing the
> > exception and then I would like to go back to try block, and
> > execute some_function() again. Is that doable?
> 
> If you know what exception to expect, and you know how to "fix" the
> cause, why not just put tests _before_ some_function() is called to
> ensure that everything is as it needs to be?

This is LBYL ("Look Before You Leap") programming style, and is
contrasted with EAFP ("it is Easier to Ask Forgiveness than
Permission") style.

    <URL:http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html>

EAFP is usually considered more Pythonic, and usually results in
smaller, clearer code.

I'm currently undecided in this case whether EAFP is correct — though
I suspect it is, as in most cases. I wanted to address your "why not
LBYL" question though.

-- 
 \          “Courage is not the absence of fear, but the decision that |
  `\             something else is more important than fear.” —Ambrose |
_o__)                                                          Redmoon |
Ben Finney



More information about the Python-list mailing list