繁体   English   中英

Python如何从“全部捕获”中排除异常

[英]Python how to exclude exceptions from “catch all”

可以说我有这个:

try:
    result = call_external_service()
    if not result == expected:
        raise MyException()
except MyException as ex:
    # bubble up
    raise ex
except Exception:
    # unexpected exceptions from calling external service
    do_some_logging()

由于我对python的了解有限,所以我想不出一种优雅的方法来MyException异常,我希望可以做类似的事情:

try:
    result = call_external_service()
    if not result == expected:
        raise MyException()
except Exception, exclude(MyException):
    # unexpected exceptions from calling external service
    do_some_logging()

您的问题似乎是您在try块中包装了太多代码。 那这个呢?:

try:
    result = call_external_service()
except Exception:
    # unexpected exceptions from calling external service
    do_some_logging()

if result != expected:
    raise MyException()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM