如何从异常组中获取异常?

问题描述 投票:0回答:1

如果我有

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")])
python python-3.x python-trio
1个回答
0
投票

这对我有用:

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]
获得该组中的第一个异常。

© www.soinside.com 2019 - 2024. All rights reserved.