GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Documentation for except* clause and ExceptionGroup can be improved . · Issue #98172 · python/cpython

Documentation for except* clause and ExceptionGroup can be improved . #98172

New issue
Copy link
New issue
Copy link
Closed
Closed
Documentation for except* clause and ExceptionGroup can be improved .#98172
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir

Description

@mrolle45
mrolle45
opened on Oct 11, 2022
Issue body actions

except*

except * currently reads:

The except* clause(s) are used for handling ExceptionGroups. The exception type for matching is interpreted as in the case of except, but in the case of exception groups we can have partial matches when the type matches some of the exceptions in the group. This means that multiple except* clauses can execute, each handling part of the exception group. Each clause executes once and handles an exception group of all matching exceptions. Each exception in the group is handled by at most one except* clause, the first that matches it.

try:
raise ExceptionGroup("eg",
[ValueError(1), TypeError(2), OSError(3), OSError(4)])
except* TypeError as e:
print(f'caught {type(e)} with nested {e.exceptions}')
except* OSError as e:
print(f'caught {type(e)} with nested {e.exceptions}')

caught <class 'ExceptionGroup'> with nested (TypeError(2),)
caught <class 'ExceptionGroup'> with nested (OSError(3), OSError(4))

  • Exception Group Traceback (most recent call last):
    | File "", line 2, in
    | ExceptionGroup: eg
    +-+---------------- 1 ----------------
    | ValueError: 1
    +------------------------------------

The following text appears in the same box as the example code, so you want to remove whatever caused that to occur in the source for the doc.

Any remaining exceptions that were not handled by any :keyword:!except*
clause are re-raised at the end, combined into an exception group along with
all exceptions that were raised from within :keyword:!except* clauses.
An :keyword:!except* clause must have a matching type,
and this type cannot be a subclass of :exc:BaseExceptionGroup.
It is not possible to mix :keyword:except and :keyword:!except*
in the same :keyword:try.
:keyword:break, :keyword:continue and :keyword:return
cannot appear in an :keyword:!except* clause.

What is needed to be explained:

  • When the try body raises a single exception, not an ExceptionGroup, it will be handled by at most one handler (the first one that matches).
  • The except* clause handles an exception group of all matching exceptions which were not already matched in an earlier except* clause.
  • Note that ExceptionGroup is a subclass of BaseExceptionGroup, and so cannot be the type in an except* clause.

ExceptionGroup

ExceptionGroup extends both BaseExceptionGroup and Exception.

BaseException

  • BaseExceptionGroup
    • ExceptionGroup
  • ...
  • Exception
    • ExceptionGroup
  • ...

Activity

  1. mrolle45
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Oct 11, 2022
  2. JelleZijlstra
    added a commit that references this issue on Oct 11, 2022

    gh-98172: Fix formatting in `except*` docs

    d02be10
  3. JelleZijlstra commented on Oct 11, 2022

    @JelleZijlstra
    JelleZijlstra
    on Oct 11, 2022
    Member
    More actions

    I opened #98173 to fix the formatting issue.

  4. iritkatriel
    changed the title [-]except* clause and ExceptionGroup need more work.[/-] [+]Documentation for except* clause and ExceptionGroup can be improved .[/+] on Oct 11, 2022
  5. JelleZijlstra
    added a commit that references this issue on Oct 11, 2022

    gh-98172: Fix formatting in `except*` docs (#98173)

    5ecf961
  6. miss-islington
    added 2 commits that reference this issue on Oct 11, 2022

    pythongh-98172: Fix formatting in `except*` docs (pythonGH-98173)

    7b71792

    gh-98172: Fix formatting in `except*` docs (GH-98173)

    9b12084
  7. JelleZijlstra commented on Oct 12, 2022

    @JelleZijlstra
    JelleZijlstra
    on Oct 12, 2022
    Member
    More actions

    I fixed the formatting issue, thanks for pointing that out!

    Your points about the substance of the docs remain. Feel free to submit a PR with improvements.

  8. carljm
    added 2 commits that reference this issue on Oct 14, 2022

    Merge branch 'main' into typewatch

    6e53514

    Merge branch 'main' into cached_property

    0bb0c69
  9. iritkatriel commented on Oct 20, 2022

    @iritkatriel
    iritkatriel
    on Oct 20, 2022
    Member
    More actions

    For except*:

    When the try body raises a single exception, not an ExceptionGroup, it will be handled by at most one handler (the first one that matches).

    Agreed, I will add this.

    The except* clause handles an exception group of all matching exceptions which were not already matched in an earlier except* clause.

    This is already stated: "Each exception in the group is handled by at most one :keyword:!except* clause, the first that matches it."

    Note that ExceptionGroup is a subclass of BaseExceptionGroup, and so cannot be the type in an except* clause.

    I don't think this is needed here (this is a language reference, not a tutorial, and the exception hierarchy is documented).

    ExceptionGroup extends both BaseExceptionGroup and Exception.

    This is why BaseExceptionGroup is in square brackets after ExceptionGroup:

    "ExceptionGroup [BaseExceptionGroup]"

    This is a text diagram, not sure how else we could do it.

  10. iritkatriel
    added 3 commits that reference this issue on Oct 20, 2022

    pythongh-98172: mention that except* handles naked exceptions

    6fddcf8

    pythongh-98172: mention that except* handles naked exceptions

    ff59828

    gh-98172: [doc] mention that except* handles naked exceptions (GH-98496)

    8367ca1
  11. miss-islington
    added a commit that references this issue on Oct 21, 2022

    pythongh-98172: [doc] mention that except* handles naked exceptions (p…

    37d2e02
  12. iritkatriel commented on Oct 21, 2022

    @iritkatriel
    iritkatriel
    on Oct 21, 2022
    Member
    More actions

    Thank you @mrolle45 .

  13. iritkatriel
    closed this as completedon Oct 21, 2022
  14. miss-islington
    added a commit that references this issue on Oct 21, 2022

    gh-98172: [doc] mention that except* handles naked exceptions (GH-98496)

    0bc2cf9
  15. pablogsal
    added a commit that references this issue on Oct 22, 2022

    gh-98172: Fix formatting in `except*` docs (GH-98173)

    e0260d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.