Returning to 'try' block after catching an exception

K

Karlo Lozovina

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.
 
A

André

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?

How about something like the following (untested)

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

André
 
K

Karlo Lozovina

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
;).
 
T

Terry Reedy

| |
| > 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()

???
 
B

bukzor

Sure, that works, but I was aiming for something more elegant and Pythonic
;).

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web:http://www.mosor.net|| ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|

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.
 
A

alex23

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?
 
K

Karlo Lozovina

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?

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.
 
K

Karlo Lozovina

Catching only the specific exceptions you think you can handle would
be more Pythonic: that way things like sys.exit() will still work
inside some_function.

I know, this was just a somewhat poorly example ;).
I prefer a 'for' loop rather than 'while' so I can limit the number of
retries.

The following may or may not be 'more pythonic', but is something I've
used. It's a factory to generate decorators which will retry the
decorated function. You have to specify the maximum number of retries,
the exceptions which indicate that it is retryable, and an optional
filter function which can try to do fixups or just specify some
additional conditions to be tested.

Interesting approach, I think I'll use something like that for avoding
infinite loops. Thanks a lot...
 
A

alex23

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.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top