gpt4 book ai didi

python - Python 中的条件 except 语句

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:51 25 4
gpt4 key购买 nike

有没有办法使 except 语句有条件 - 换句话说,只有在条件为真时才捕获一种类型的异常?这是我的想法,但似乎行不通

try:
<code>
if condition == True:
except Exception as e:
<error handling>

最佳答案

不,你不能那样做。您可以做的是捕获错误,然后检查 except block 内的条件,并在必要时重新引发:

except Exception as e:
if not condition:
raise

关于python - Python 中的条件 except 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48689723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com