Chain exceptions where appropriate
See original GitHub issuePicking 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:
- 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
- The inner error provides no valuable information that is not present in the outer error. We should use
from None
:
In that example all the information from thetry: some_dict[name] except KeyError: raise ValueError("The name %s is not supported" % name) from None
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:
- Created 3 years ago
- Reactions:1
- Comments:26 (16 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s very marginally useful, as it removes unhelpful wording from tracebacks like “another exception occurred”, and removes
KeyError
s 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.
… 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: