Skip to content

Tutorial's claim about default exception chaining misleading #97654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smheidrich opened this issue Sep 29, 2022 · 3 comments · Fixed by #97703
Closed

Tutorial's claim about default exception chaining misleading #97654

smheidrich opened this issue Sep 29, 2022 · 3 comments · Fixed by #97703
Assignees
Labels
docs Documentation in the Doc dir

Comments

@smheidrich
Copy link
Contributor

Documentation

The tutorial's section on exception chaining (raise x from y) currently claims:

Exception chaining happens automatically when an exception is raised inside an except or finally section.

While that is true, the semantics of automatically chained exceptions are different from ones explicitly chained with from, which is also reflected in how their relation is described in the resulting error messages: Automatically chained exceptions are linked with

During handling of the above exception, another exception occurred:

while for explicitly chained exceptions it says

The above exception was the direct cause of the following exception:

That is not mentioned anywhere in the tutorial section, making the claim that "chaining happens automatically" by itself extremely misleading because it suggests to people they'll get the same result whether they chain explicitly with from or not, which isn't the case. It's probably also confusing to those readers who give it a bit more thought, because the natural question to ask would be "Then why does from exist at all if it happens automatically anyway?"

My suggestion would be to explain the semantic difference and the different resulting outputs in the tutorial section, e.g. like this:

A kind of exception chaining also happens automatically when an exception is raised inside an except or finally section, although in this case the statement linking the exceptions in the error message will be different:

>>> try:
...     func()
... except ConnectionError as exc:
...     raise RuntimeError
...
Traceback (most recent call last):
 File "<stdin>", line 2, in <module>
 File "<stdin>", line 2, in func
ConnectionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "<stdin>", line 4, in <module>
RuntimeError

As can be seen from the output, an exception raised inside an exception handler but not explicitly chained with from represents an additional, possibly unrelated error that occurred during handling, while an explicitly chained exception represents a direct consequence of the caught exception.

Of course the phrasing isn't perfect but that can be fought over in a PR if you think the overall point of this issue is valid.

Related issues

@smheidrich smheidrich added the docs Documentation in the Doc dir label Sep 29, 2022
@stevendaprano
Copy link
Member

It is a tutorial, not the reference manual. It doesn't have to dive deep into every facet of the feature.

As you say yourself, it "is true" that exceptions are chained automatically, exactly as the tutorial says.

I take your point about the (very slight) difference in intended semantics of the two situations, but they aren't two different kinds of chaining. There is only one kind of exception chaining, which differ only in whether the link between one exception and the other is described as a cause or not. Since the chain can be an arbitrary number of exceptions long, it can contain an arbitrary mix of exceptions with the cause or the context set.

the claim that "chaining happens automatically" by itself extremely misleading because it suggests to people they'll get the same result whether they chain explicitly with from or not

"Extremely" misleading? Over a slight difference in wording, that most people will neither notice nor care about? I don't think it is misleading at all, let alone "extremely" misleading. I think that dealing with this subtle difference as part of the tutorial is unnecessary detail for the tutorial's intended audience.

@smheidrich
Copy link
Contributor Author

smheidrich commented Sep 30, 2022

@stevendaprano So why have from be part of the tutorial at all if the wording that links exceptions in the output doesn't matter? That's all it does after all. If it truly didn't matter and the correct thing to teach people was to just not use it (which the "happens automatically" sentence suggests anyway), arguably it shouldn't even be in there. But if it's important enough to warrant its own keyword and its own tutorial section, I'd say it's important enough to explain in this section what the actual difference is between using and not using it.

Maybe another way to approach this would be to first have a paragraph either in that section or in an earlier one that shows what happens normally when an exception is raised from inside an except block (i.e. the automatic/implicit chaining), no matter whether it originates from a raise without from or another (non-raise) statement in the block. Then the explanation of explicit exception chaining with from doesn't need the "happens automatically" sentence anymore (because that has already been shown) and the subsequent explanation of from None to suppress implicit chaining can refer to that previous paragraph. It also wouldn't have to go into detail about the semantic difference then because that would be implied. I submitted a PR to illustrate how this could look.

JelleZijlstra pushed a commit that referenced this issue Oct 5, 2022
Add auto exception chaining example to tutorial
@JelleZijlstra JelleZijlstra reopened this Oct 5, 2022
@JelleZijlstra JelleZijlstra self-assigned this Oct 5, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 5, 2022
…onGH-97703)

Add auto exception chaining example to tutorial
(cherry picked from commit 395b66a)

Co-authored-by: Shahriar Heidrich <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 5, 2022
…onGH-97703)

Add auto exception chaining example to tutorial
(cherry picked from commit 395b66a)

Co-authored-by: Shahriar Heidrich <[email protected]>
ambv pushed a commit that referenced this issue Oct 5, 2022
…97703) (#97885)

gh-97654: Add auto exception chaining example to tutorial (GH-97703)

Add auto exception chaining example to tutorial
(cherry picked from commit 395b66a)

Co-authored-by: Shahriar Heidrich <[email protected]>
ambv pushed a commit that referenced this issue Oct 5, 2022
…97703) (#97884)

Add auto exception chaining example to tutorial
(cherry picked from commit 395b66a)

Co-authored-by: Shahriar Heidrich <[email protected]>
@JelleZijlstra
Copy link
Member

Backports were merged, thank you!

mpage pushed a commit to mpage/cpython that referenced this issue Oct 11, 2022
pablogsal pushed a commit that referenced this issue Oct 22, 2022
…97703) (#97885)

gh-97654: Add auto exception chaining example to tutorial (GH-97703)

Add auto exception chaining example to tutorial
(cherry picked from commit 395b66a)

Co-authored-by: Shahriar Heidrich <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants