I would like to look for a better way to handle the following exception,
def fun(i, j, k, l):
try:
do_something_1(i)
except TimeoutException as f:
try:
do_something_2(j)
except TimeoutExeption as e:
try:
do_something_3(k)
except TimeoutExeption as e:
try:
do_something_4(l)
except TimeoutExeption as e:
raise Exception
The basic idea is to try something, if it doesn't work, try next thing and so on until it gets what it wants or fails. But it has to follow the order of execution.
How can I make it better?
Probably use a loop?
def fun(i):
errors = []
things_to_try = (thing1, thing2, thing3, thing4)
for thing in things_to_try:
try:
thing(i)
except Exception as e:
errors.append(e)
else:
break
else:
raise Exception("Failed: %s" % errors)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments