diff options
author | Kinshuk Dua <kinshukdua@gmail.com> | 2022-01-27 10:24:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-27 10:24:48 (GMT) |
commit | 08c0ed2d9c0d01ad1a5adc0787bc75e4e90cbb85 (patch) | |
tree | bcb78c4f5b61fb3345fd7e711fb638f7c823457f /Doc/reference | |
parent | 606e496dd6e2ace298532da200169124c26ae0f2 (diff) | |
download | cpython-08c0ed2d9c0d01ad1a5adc0787bc75e4e90cbb85.zip cpython-08c0ed2d9c0d01ad1a5adc0787bc75e4e90cbb85.tar.gz cpython-08c0ed2d9c0d01ad1a5adc0787bc75e4e90cbb85.tar.bz2 |
bpo-23556: [doc] Fix inaccuracy in documentation for raise without args. Improve tests for context in nested except handlers. (GH-29236)
Diffstat (limited to 'Doc/reference')
-rw-r--r-- | Doc/reference/simple_stmts.rst | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst index 3d02074..e9795d8 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -563,10 +563,10 @@ The :keyword:`!raise` statement .. productionlist:: python-grammar raise_stmt: "raise" [`expression` ["from" `expression`]] -If no expressions are present, :keyword:`raise` re-raises the last exception -that was active in the current scope. If no exception is active in the current -scope, a :exc:`RuntimeError` exception is raised indicating that this is an -error. +If no expressions are present, :keyword:`raise` re-raises the +exception that is currently being handled, which is also known as the *active exception*. +If there isn't currently an active exception, a :exc:`RuntimeError` exception is raised +indicating that this is an error. Otherwise, :keyword:`raise` evaluates the first expression as the exception object. It must be either a subclass or an instance of :class:`BaseException`. @@ -581,8 +581,8 @@ The :dfn:`type` of the exception is the exception instance's class, the A traceback object is normally created automatically when an exception is raised and attached to it as the :attr:`__traceback__` attribute, which is writable. You can create an exception and set your own traceback in one step using the -:meth:`with_traceback` exception method (which returns the same exception -instance, with its traceback set to its argument), like so:: +:meth:`~BaseException.with_traceback` exception method (which returns the +same exception instance, with its traceback set to its argument), like so:: raise Exception("foo occurred").with_traceback(tracebackobj) @@ -614,8 +614,10 @@ exceptions will be printed:: File "<stdin>", line 4, in <module> RuntimeError: Something bad happened -A similar mechanism works implicitly if an exception is raised inside an -exception handler or a :keyword:`finally` clause: the previous exception is then +A similar mechanism works implicitly if a new exception is raised when +an exception is already being handled. An exception may be handled +when an :keyword:`except` or :keyword:`finally` clause, or a +:keyword:`with` statement, is used. The previous exception is then attached as the new exception's :attr:`__context__` attribute:: >>> try: |