繁体   English   中英

如何在 Python 中的 if/else 语句中退出 try/except

[英]How to exit from a try/except inside of an if/else statement in Python

所以我有这个:

if condition:
    ...
elif condition2:
    ...
elif condition3:
    try:
        existing_bid = # get some object from django models 
    except SomeError: # if this object does not exist
        # here I need to escape from if/else to main scope
    if ... # some comparison with an existing_bid:
        #return error
    ... # try to manipulate existing_bid object and save it
# here I need to escape
...
...

什么是从尝试/除了在主 scope 的 if/else 内部逃脱的最佳实践? 谢谢

编辑:
提供了一些上下文

如果你想在 try/except 之后逃跑并回到主 scope。 如果将 try/except 语句放在elif条件 3 的底部,将很容易处理。一旦完成try/except语句,程序也会完成elif语句并转义到主 scope。

否则,你必须在你的try/except代码中break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM