Am I the only one who finds Python's "forwards" backtraces confusing compared to every other language I've used? I can't help but think at least part of this confusion is because of that.
That said:
* Would printing the trace as a tree actually clarify anything?
* Good code almost always minimizes the amount of code in an exception handler.
* `raise_exception()` is generally bad practice; consider `raise get_exception()` if you need something complicated (among others, this allows choice of `from`)
* `raise ... from None` is useful since it's often the most recent stacktrace that's actually relevant.
* `raise ... from e` likely implies that it is the older stacktrace that's most relevant; ideally this would just be a single annotation on a particular frame "exception was replaced here".
You're probably not the only one, but as a Python developer of 20 years I can assure you that the confusion _for me_ is not caused by the order of the Stacktrace itself, but specifically by the way chained exceptions are shown.
`raise_exception()` in the example is just to ensure a trivial example which still has a stacktrace in the handler (this is not uncommon in reality, though in reality the called function is not typically called with the goal of triggering yet another example)
yes - I'm currently at a new job where I work on a python back-end, but there's also java/kotlin microservices, and I do front-end web with typescript or mobile with java/kotlin
Wait...why is the OriginalException shown? The try catch already handled it, and the second exception doesn't "use" it and instead create a new one...it shouldn't be shown.
Is python automatically chaining the exceptions itself in a non-explicit manner?
Yes, the article doesn't mention this but indeed, in Python you can explicitly chain exceptions, which changes the message into "The above exception was the direct cause of the following exception:"
You can also disable this behavior using the `from None` syntax.
I used to be one of those kids that hated on Java for being boring, until I spent six years doing nothing but deep learning in Python and C++. I miss you, Gradle…
Am I the only one who finds Python's "forwards" backtraces confusing compared to every other language I've used? I can't help but think at least part of this confusion is because of that.
That said:
* Would printing the trace as a tree actually clarify anything?
* Good code almost always minimizes the amount of code in an exception handler.
* `raise_exception()` is generally bad practice; consider `raise get_exception()` if you need something complicated (among others, this allows choice of `from`)
* `raise ... from None` is useful since it's often the most recent stacktrace that's actually relevant.
* `raise ... from e` likely implies that it is the older stacktrace that's most relevant; ideally this would just be a single annotation on a particular frame "exception was replaced here".
You're probably not the only one, but as a Python developer of 20 years I can assure you that the confusion _for me_ is not caused by the order of the Stacktrace itself, but specifically by the way chained exceptions are shown.
`raise_exception()` in the example is just to ensure a trivial example which still has a stacktrace in the handler (this is not uncommon in reality, though in reality the called function is not typically called with the goal of triggering yet another example)
yes - I'm currently at a new job where I work on a python back-end, but there's also java/kotlin microservices, and I do front-end web with typescript or mobile with java/kotlin
python is the only one doing this weird crap
Wait...why is the OriginalException shown? The try catch already handled it, and the second exception doesn't "use" it and instead create a new one...it shouldn't be shown.
Is python automatically chaining the exceptions itself in a non-explicit manner?
Edit: found the PEP: https://peps.python.org/pep-3134/
Yes, the article doesn't mention this but indeed, in Python you can explicitly chain exceptions, which changes the message into "The above exception was the direct cause of the following exception:"
You can also disable this behavior using the `from None` syntax.
https://docs.python.org/3/tutorial/errors.html#exception-cha...
I used to be one of those kids that hated on Java for being boring, until I spent six years doing nothing but deep learning in Python and C++. I miss you, Gradle…