[英]How to exit from a try/except inside of an if/else statement in Python
So I have this:所以我有这个:
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
...
...
What is the best practice to escape from try/except that is inside of an if/else to main scope?什么是从尝试/除了在主 scope 的 if/else 内部逃脱的最佳实践? Thanks谢谢
edit:编辑:
provided some context提供了一些上下文
If you want to escape after the try/except and back to the main scope.如果你想在 try/except 之后逃跑并回到主 scope。 It will be easy to handle if you put the try/except statement at the bottom of your elif
condition 3. Once it finishes the try/except
statement, the program will also finish the elif
statement and escape to the main scope.如果将 try/except 语句放在elif
条件 3 的底部,将很容易处理。一旦完成try/except
语句,程序也会完成elif
语句并转义到主 scope。
Otherwise, you have to put a break
in your try/except
code.否则,你必须在你的try/except
代码中break
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.