Python:在捕获并解决异常后再次运行 "try"

标签 python

有没有办法在第一次 try catch 异常后再次进入try语句?
现在我正在使用“while”和“if”语句,它使代码变得困惑。
有任何想法吗?
会尽量简化它,对不起,没有逻辑......

run = True
tryAgain = True
a=0
while run:
try:
    2/a
except Exception:
    if tryAgain:
        tryAgain = False
        a = 1
    else:
        run = False

最佳答案

您可以尝试使用 break您在 try 中的声明堵塞:

while True:
    try:
        # try code
        break # quit the loop if successful
    except:
        # error handling

关于Python:在捕获并解决异常后再次运行 "try",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36142149/

相关文章:

函数的 Python unittest.mock.patch 不起作用

python - 似乎无法在此列表中最后添加百分比?

python - 如何使用 Python 3 从不同目录导入模块?

python - pip 和 pip3 - 都指向 python3.5?

python - 比单个列一次跨数据框剥离空间的优雅方式

python - 如何打包一个程序分享给大家?

python - 在带有 OneVsRestClassifier 的 Neuraxle Pipeline 中使用 Predict_proba() 代替 Predict()

python - 在Python中组合两个字节

python - 包含 4 个元素的数组 : need to compare max+min and the other 2 elements

python - scikit-learn 查询数据维度必须匹配训练数据维度