如果我有
ExceptionGroup
,我如何从中取出 Exception
?我碰巧正在使用 Trio。
>>> exc
>>> ExceptionGroup('Exceptions from Trio nursery', [HTTPStatusError("Client error
'400 Bad Request' for url 'https://api.fastly.com/service/xyz/acl/xyz/entries'
\nFor more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400")])
这对我有用:
except* HTTPError as exc:
httpexps, others = exc.split(HTTPError)
httpexp = httpexps.exceptions[0]
if httpexp.response.status_code == 400:
# etc etc
except* HTTPError
捕获任何带有 HTTPError
的组。
exc.split(HTTPError)
将组分成两个新组,一组仅包含 HTTPError
,一组包含其他所有内容。
httpexps.exceptions[0]
获得该组中的第一个异常。