Concealing an exception root cause
In Python 3, exceptions contain a root cause. The default behavior of internally raised exceptions is to use an implicit
__context__ to include the root cause of an exception. In some cases, we may want to deemphasize the root cause because it's misleading or unhelpful for debugging.
This technique is almost always paired with an application or library that defines a unique exception. The idea is to show the unique exception without the clutter of an irrelevant exception from outside the application or library.
Assume we're writing some complex string processing. We'd like to treat a number of different kinds of detailed exceptions as a single generic error so that users of our software are insulated from the implementation details. We can attach details to the generic error.
How to do it...
- To create a new exception, we can do this:
>>> class MyAppError(Exception): .....