Returning to 'try' block after catching an exception

Discussion in 'Python' started by Karlo Lozovina, May 22, 2008.

  1. I'm not sure if Python can do this, and I can't find it on the web. So,
    here it goes:


    try:
    some_function()
    except SomeException:
    some_function2()
    some_function3()
    ...
    # somehow goto 'try' block again

    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?

    Thanks.
     
    Karlo Lozovina, May 22, 2008
    #1
    1. Advertisements

  2. Karlo Lozovina

    André Guest

    How about something like the following (untested)

    done = False
    while not done:
    try:
    some_function()
    done = True
    except:
    some_function2()
    some_function3()

    André
     
    André, May 22, 2008
    #2
    1. Advertisements

  3. Sure, that works, but I was aiming for something more elegant and Pythonic
    ;).
     
    Karlo Lozovina, May 22, 2008
    #3
  4. Karlo Lozovina

    Terry Reedy Guest

    | |
    | > How about something like the following (untested)
    | >
    | > done = False
    | > while not done:
    | > try:
    | > some_function()
    | > done = True
    | > except:
    | > some_function2()
    | > some_function3()
    |
    | Sure, that works, but I was aiming for something more elegant and
    Pythonic
    | ;).

    while True:
    try:
    some_function()
    break
    except Exception:
    patchup()

    ???
     
    Terry Reedy, May 22, 2008
    #4
  5. Karlo Lozovina

    bukzor Guest

    It's hard to get around a while loop if you want to conditionally
    repeat something. There's no built-in way to do what you ask.
     
    bukzor, May 22, 2008
    #5
  6. Karlo Lozovina

    alex23 Guest

    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?
     
    alex23, May 22, 2008
    #6
  7. Because when you expect exception to occur on something like 0.01% of
    cases, and you have 4 or 5 exceptions and the code to test for each
    conditions that cause exceptions is quite long and burried deep inside
    some other code it's much better to do it this way ;). Too bad there's no
    syntactic sugar for doing this kind of try-except loop.
     
    Karlo Lozovina, May 22, 2008
    #7
  8. I know, this was just a somewhat poorly example ;).
    Interesting approach, I think I'll use something like that for avoding
    infinite loops. Thanks a lot...
     
    Karlo Lozovina, May 22, 2008
    #8
  9. Karlo Lozovina

    alex23 Guest

    I'm surprised your unit tests let it get to such a state... ;)

    How about something like this?

    retry, total_fail = False, False
    try:
    some_function()
    except SomeException:
    some_function2()
    some_function3()
    retry = True
    finally:
    if retry:
    try:
    some_function()
    except SomeException:
    total_fail = True

    Using 'finally' seems more explicit about it being part of the
    exception handling than using a loop construct.

    Actually, this is even more direct:

    try:
    some_function()
    except SomeException:
    some_function2()
    some_function3()
    try:
    some_function()
    except SomeException:
    raise SomeError
     
    alex23, May 22, 2008
    #9
  10. Karlo Lozovina

    inhahe Guest

    Might have a stack overflow issue, if it retries too many times?
     
    inhahe, May 22, 2008
    #10
  11. In which example? Neither of them is looping...
     
    Matthew Trevor, May 23, 2008
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.