请考虑以下代码段:
all_errors = []
for i in something:
try:
do_something_that_throws(i)
except Exception as e
# what I know I can do:
raise MyCustomException(i) from e
# what I actually want:
# all_errors.append(MyCustomException(i) from e)
有没有一种方法可以使用from e
对我有用的所有初始化(设置__cause__
或其他设置)构造MyCustomException ,但又不会抛出该初始化?
AFAIK,除了__cause__
手动设置没有其他方法。
但是,由于您创建了自定义异常,因此这可能会有所帮助:
class MyCustomException(Exception):
def __init__(self, cause=None)
self.__cause__ = cause
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句