我想寻找更好的方法来处理以下异常
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
基本的想法是尝试一些东西,如果它不起作用,尝试下一件事,等等,直到它得到它想要的或失败。但它必须遵循执行顺序
我怎样才能做得更好
使用for循环:
可能使用循环
您可以使用
return
语句尽早退出函数,以避免嵌套。如果发生异常时没有其他操作,请使用pass
作为空语句,让下面的执行继续对于最后一个步骤,
return
实际上并不是必需的,但您可以保留它以保持一致性,并避免以后添加更多步骤时出现错误相关问题 更多 >
编程相关推荐