Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python尝试/例外后继续执行脚本_Python - Fatal编程技术网

使用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')

您捕获了错误的异常。这很尴尬…它应该是异常而不是错误。非常感谢。