让我说我有这个:
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()
答案 0 :(得分:2)
您的问题似乎是您在try块中包含了太多代码。那怎么样?:
try:
result = call_external_service()
except Exception:
# unexpected exceptions from calling external service
do_some_logging()
if result != expected:
raise MyException()