【发布时间】:2020-11-26 17:46:43
【问题描述】:
我的代码结构如下:
try:
x = function_one(args)
try:
y = function_two(args)
try:
#
# some code where I need x and y
#
except Exception as e::
print("Problem with code above : ", e)
except Exception as e:
print("Problem with function_two : ", e)
#
# some code here
#
except Exception as e:
print("Problem with function_one : ", e)
它有效,但我想知道我是否可以避免这种嵌套的尝试/异常?
例如,如果 x 为空并且之后不能使用,最好将 try / except 放在 function_one 中并找到一个解决方案来检查我是否可以将 x 用于其余代码,如果不能,则停止代码 ?
我可以做一个if x something,但它也会创建嵌套部分。
【问题讨论】:
标签: python python-3.x error-handling