question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chain exceptions where appropriate

See original GitHub issue

Picking up from #15731, there are many places in numpy where we do something like:

try:
    something_which_throws_typeError
except TypeError:
    raise ValueError("a clearer explanation of what went wrong")

It would produce a marginally clearer error if we could change these to use traceback chaining. Our two options are either:

  1. The inner error provides valuable information that is not present in the outer error. We should use from e.
    try:
        something_which_throws_typeError
    except TypeError as e:
        raise ValueError("a clearer explanation of what went wrong") from e
    
  2. The inner error provides no valuable information that is not present in the outer error. We should use from None:
    try:
        some_dict[name]
    except KeyError:
        raise ValueError("The name %s is not supported" % name) from None
    
    In that example all the information from the KeyError is already in the message of the new error, so there is no point chaining

An example of such a change would be this line of this otherwise-discarded patch.

For now, I would recommend we only apply this to cases where the exception is thrown unconditionally, as other cases can be more nuanced.


Please see this list for potential places to contribute. Special thanks to @nkleinbaer for compiling the list!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:26 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
eric-wiesercommented, Mar 13, 2021

It’s very marginally useful, as it removes unhelpful wording from tracebacks like “another exception occurred”, and removes KeyErrors internal to numpy that are just an implementation convenience. I regular see Python novices scared enough by a long traceback that they just read none of it.

This issue seems to be effective at drawing in new contributors, although I haven’t kept track of whether any of them go on to contribute further.

2reactions
mattipcommented, Jun 9, 2021

… with that please realize that this is a low priority issue, so PRs may take a while to get reviewed and merged. And as I commented above:

We would like to suggest that contributors show the output from the error. This will require you think about the error: is the code path actually hit? Does it make sense to chain the exception?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chained Exceptions in Java - Baeldung
In this article, we'll have a very brief look at what Exception is and go in depth about discussing the chained exceptions in...
Read more >
Chained Exceptions in Java - GeeksforGeeks
Chained Exceptions allows to relate one exception with another exception, i.e one exception describes cause of another exception.
Read more >
Chained Exceptions - The Java™ Tutorials - Oracle Help Center
This Java tutorial describes exceptions, basic input/output, concurrency, regular expressions, and the platform environment.
Read more >
Understanding Java Exception Chaining with Code Examples
The main purpose of exception chaining is to preserve the original exception when it propagates across multiple logical layers in a program.
Read more >
What is exception chaining in Java? - Educative.io
Exception chaining occurs when one exception causes another exception. The original exception is the cause of the second exception. In Java, a chained...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found