#python – How to re-raise an exception in nested try/except blocks Techhelpnotes
Explore tagged Tumblr posts
techhelpnotes · 3 years ago
Text
python – How to re-raise an exception in nested try/except blocks?
As of Python 3 the traceback is stored in the exception, so a simple raise e will do the (mostly) right thing:
The traceback produced will include an additional notice that SomeError occurred while handling AlsoFailsError (because of raise e being inside except AlsoFailsError). This is misleading because what actually happened is the other way around – we encountered AlsoFailsError, and handled it, while trying to recover from SomeError. To obtain a traceback that doesnt include AlsoFailsError, replace raise e with raise e from None.
0 notes