4 ms·
Some good practices, reminds of Effective Java. Couple nuggets as well: * just `raise` implies re-raising the current exception * `raise MyException() fro
by nighthawk454 5y ago
Some good practices, reminds of Effective Java.
Couple nuggets as well:
* just `raise` implies re-raising the current exception
* `raise MyException() from ex` continues the stack trace
* `logger.info(..., exec_info=True)` can add a stack trace to non-error level logs
- stevula 5y agoException chaining (`raise from`) was new to me but after reading the docs it seems to be the default behavior in except/finally blocks, so the examples from the article don’t actually add anything. It doesn’t hurt to be explicit though. https://docs.python.org/3/tutorial/errors.html#exception-chaining https://docs.python.org/3/tutorial/errors.html#exception-cha...
- Doxin 5y agoUsing "raise foo from bar" changes the message between the exceptions from "During handling of the above exception, another exception occurred:" to "The above exception was the direct cause of the following exception:" So yes, these days the whole "raise from" construct is nearly pointless. Used to be that exception chaining wasn't automatic though.
- aidos 5y agoI believe there’s a subtle difference in the meanings. You can raise or raise from. In either case the chaining means you get to see both messages. But one implies the original error is the cause. It’s so subtle that I don’t recall which is which!