我正在与引发如下异常的API进行通信:raise Exception("end of time")
。如何根据Python 3中的字符串参数来捕获一般异常?
只有当字符串参数是“结束时间”时,我怎样才能捕获Exception
?
我不想做的事:
except Exception:
pass
我想要做的事,如:
except Exception("end of time"):
pass
然而,这并不能捕获异常。
例回溯:
File "test.py", line 250, in timeout_handler
raise Exception("end of time")
Exception: end of time
@vaultah重复问题的链接是不恰当的,因为答案不适用于Python 3. – user8615908
增加了另一个。希望mgilson会更新他的答案。 – vaultah