Returning to 'try' block after catching an exception
alex23
wuwei23 at gmail.com
Thu May 22 04:37:43 EDT 2008
On May 22, 6:15 pm, Karlo Lozovina <_karlo_ at _mosor.net_> wrote:
> 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
More information about the Python-list
mailing list