6

Consider this snippet:

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)

Is there a way to construct MyCustomException with all the initializations that from e does for me (setting __cause__ or what ever else), but without throwing the it?

1 Answer 1

5

AFAIK, there is no other way than to set __cause__ manually.

But since you create a custom exception, this might be helpful:

class MyCustomException(Exception):
    def __init__(self, cause=None)
        self.__cause__ = cause
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.