A Stacktrace Puzzle

(bugsink.com)

10 points | by thunderbong 5 hours ago ago

6 comments

  • o11c 3 hours ago

    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".

    • vanschelven 22 minutes ago

      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)

    • silenced_trope 3 hours ago

      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

  • TrianguloY 43 minutes ago

    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/

  • lqstuart an hour ago

    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…