python - Python try/except取决于变量

标签 python if-statement error-handling try-except

我需要在程序之后继续执行,除非变量为True,但变量为False时需要退出程序。
我认为还会有其他情况,但我不确定如何使用它。

例如:

var = True

try:
    print 2/0
except:
    exit(1)

... continue executing
var = False

try:
    print 2/0
except:
    exit(1)

... exit 

感谢您的评论。

最佳答案

这应该可以解决问题,因为您可能应该使用抬高。

var = True

try:
    print 2/0
except:
    if not var:
        # I recommend using raise, as it would show you the error
        exit(1)

关于python - Python try/except取决于变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35210530/

相关文章:

python - 是否可以在每个训练步骤中获得目标函数值?

c - 我想用c程序解决这个查询

javascript - 浓缩 if, else 条件相似的 JS

error-handling - 小门: Where to handle JDBC connection error

c# - 如何将MVC C#服务器错误+数字转换为行号

error-handling - NServiceBus 的一个或多个错误队列?

python - 来自 Python 的 QPX Express API

python - 如何在 python JSON 字符串中转义 true/false bool 值

python - Elastic beanstalk Django 管理 css 未加载

MySQL 创建 IF 查询以显示 Col_A 所在的层