即使发生错误仍继续运行 python 代码?

时间:2021-07-11 14:41:26

标签: python python-3.x exception try-catch

是否可以在发生错误后继续执行代码?

假设我有一个名为 app.py 的代码,其中包含以下代码 -

def call(temp):
    call=temp+1
    t=fast+1
    if temp==11:
        c=22/3
    else:
        c=1
    return 5

此处 fast 未定义,但即使发生此错误,我仍希望代码执行。

我查看了 Internet 并找到了 pass 技巧,但它在我的情况下不起作用?

import app
def test_call():
    try:
        app.call(11)
    except Exception:
        pass
test_call()

就我而言,代码只执行到 fast 行。有没有办法继续执行?

感谢并抱歉我的英语不好。

3 个答案:

答案 0 :(得分:0)

您需要在 call 函数中使用它。

def call(temp):
    call=temp+1
    try:
        t=fast+1
    except Exception:
        pass
    if temp==11:
        c=22/3
    else:
        c=1
    return 5

答案 1 :(得分:0)

这里有一些你可能会用到的信息,如果你点击'Mark all unresolved attributes of..'选项,它会解决你的问题。 link

答案 2 :(得分:-2)

在python中使用try-except条件

try:
   #any calculations
except:
   pass

pass 中的 except 只是忽略导致程序捕获异常的任何错误
此外,如果这不起作用,请参阅 here