Include the original exception

As part of our error handling logic, we might decide to raise a different one, and maybe even change its message. If that is the case, it is recommended to include the original exception that led to that.

In Python 3 (PEP-3134), we can now use the raise <e> from <original_exception> syntax. When using this construction, the original traceback will be embedded into the new exception, and the original exception will be set in the __cause__ attribute of the resulting one.

For example, if we desire to wrap default exceptions with custom ones internally to our project, we could still do that while including information about the root exception:

class InternalDataError(Exception): """An exception with the data of ...

Get Clean Code in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.