使用Python尝试/例外后继续执行脚本
来自其他脚本语言的我可能缺少一些基本的东西 当引发异常时,如何继续处理脚本的其余部分 例如:使用Python尝试/例外后继续执行脚本,python,Python,来自其他脚本语言的我可能缺少一些基本的东西 当引发异常时,如何继续处理脚本的其余部分 例如: errMsg = None try: x = 1 y = x.non_existent_method() except ValueError as e: errMsg = str(e) if errMsg: print('Error') else: print('All good') 脚本就是失败了。有办法继续剩下的吗 提前谢谢很抱歉问了这个愚蠢的问题。 它应
errMsg = None
try:
x = 1
y = x.non_existent_method()
except ValueError as e:
errMsg = str(e)
if errMsg:
print('Error')
else:
print('All good')
脚本就是失败了。有办法继续剩下的吗
提前谢谢很抱歉问了这个愚蠢的问题。 它应该是“Exception”而不是“ValueError”
errMsg = None
try:
x = 1
y = x.non_existent_method()
except Exception as e:
errMsg = str(e)
if errMsg:
print('Error')
else:
print('All good')
您捕获了错误的异常。这很尴尬…它应该是异常而不是错误。非常感谢。