gpt4 book ai didi

python - python忽略引起错误的任何一行,并在该行之后继续运行代码

转载 作者:行者123 更新时间:2023-12-03 08:11:24 25 4
gpt4 key购买 nike

我了解try/except方法。我想做的是:

try:
some_func1() #potentially raises error too
do_something_else() #error was raised
continue_doing_something_else() #continues here after handling error
except:
pass

在上面的代码中,当在do_something_else()处引发错误时,将处理该错误,但是将退出try语句。

我想要的是让python继续执行导致错误的行之后的任何代码。假设在try语句中的任何地方都可能发生错误,所以我不能仅将try/except包装在do_something_else()本身周围,是否可以在python中做到这一点?

最佳答案

您想做的事(尝试重新启动)在Python中是不可能的。 Lisp可以做到这一点(http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html),您可以使用call/cc在Scheme中实现它。

关于python - python忽略引起错误的任何一行,并在该行之后继续运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45321314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com