@@ -199,10 +199,8 @@ returns the list ``[0, 1, 2]``.
199
199
.. versionchanged :: 3.11
200
200
Starred elements are now allowed in the expression list.
201
201
202
+
202
203
.. _try :
203
- .. _except :
204
- .. _except_star :
205
- .. _finally :
206
204
207
205
The :keyword: `!try ` statement
208
206
=============================
@@ -215,7 +213,7 @@ The :keyword:`!try` statement
215
213
keyword: as
216
214
single: : (colon); compound statement
217
215
218
- The :keyword: `try ` statement specifies exception handlers and/or cleanup code
216
+ The :keyword: `! try ` statement specifies exception handlers and/or cleanup code
219
217
for a group of statements:
220
218
221
219
.. productionlist :: python-grammar
@@ -231,40 +229,56 @@ for a group of statements:
231
229
try3_stmt: "try" ":" `suite `
232
230
: "finally" ":" `suite `
233
231
232
+ Additional information on exceptions can be found in section :ref: `exceptions `,
233
+ and information on using the :keyword: `raise ` statement to generate exceptions
234
+ may be found in section :ref: `raise `.
234
235
235
- The :keyword: `except ` clause(s) specify one or more exception handlers. When no
236
+
237
+ .. _except :
238
+
239
+ :keyword: `!except ` clause
240
+ -------------------------
241
+
242
+ The :keyword: `!except ` clause(s) specify one or more exception handlers. When no
236
243
exception occurs in the :keyword: `try ` clause, no exception handler is executed.
237
244
When an exception occurs in the :keyword: `!try ` suite, a search for an exception
238
- handler is started. This search inspects the except clauses in turn until one
239
- is found that matches the exception. An expression-less except clause, if
240
- present, must be last; it matches any exception. For an except clause with an
241
- expression, that expression is evaluated, and the clause matches the exception
245
+ handler is started. This search inspects the :keyword: `!except ` clauses in turn
246
+ until one is found that matches the exception.
247
+ An expression-less :keyword: `!except ` clause, if present, must be last;
248
+ it matches any exception.
249
+ For an :keyword: `!except ` clause with an expression,
250
+ that expression is evaluated, and the clause matches the exception
242
251
if the resulting object is "compatible" with the exception. An object is
243
252
compatible with an exception if the object is the class or a
244
253
:term: `non-virtual base class <abstract base class> ` of the exception object,
245
254
or a tuple containing an item that is the class or a non-virtual base class
246
255
of the exception object.
247
256
248
- If no except clause matches the exception, the search for an exception handler
257
+ If no :keyword: `!except ` clause matches the exception,
258
+ the search for an exception handler
249
259
continues in the surrounding code and on the invocation stack. [# ]_
250
260
251
- If the evaluation of an expression in the header of an except clause raises an
252
- exception, the original search for a handler is canceled and a search starts for
261
+ If the evaluation of an expression
262
+ in the header of an :keyword: `!except ` clause raises an exception,
263
+ the original search for a handler is canceled and a search starts for
253
264
the new exception in the surrounding code and on the call stack (it is treated
254
265
as if the entire :keyword: `try ` statement raised the exception).
255
266
256
267
.. index :: single: as; except clause
257
268
258
- When a matching except clause is found, the exception is assigned to the target
259
- specified after the :keyword: `!as ` keyword in that except clause, if present, and
260
- the except clause's suite is executed. All except clauses must have an
261
- executable block. When the end of this block is reached, execution continues
262
- normally after the entire try statement. (This means that if two nested
263
- handlers exist for the same exception, and the exception occurs in the try
264
- clause of the inner handler, the outer handler will not handle the exception.)
269
+ When a matching :keyword: `!except ` clause is found,
270
+ the exception is assigned to the target
271
+ specified after the :keyword: `!as ` keyword in that :keyword: `!except ` clause,
272
+ if present, and the :keyword: `!except ` clause's suite is executed.
273
+ All :keyword: `!except ` clauses must have an executable block.
274
+ When the end of this block is reached, execution continues
275
+ normally after the entire :keyword: `try ` statement.
276
+ (This means that if two nested handlers exist for the same exception,
277
+ and the exception occurs in the :keyword: `!try ` clause of the inner handler,
278
+ the outer handler will not handle the exception.)
265
279
266
280
When an exception has been assigned using ``as target ``, it is cleared at the
267
- end of the except clause. This is as if ::
281
+ end of the :keyword: ` ! except` clause. This is as if ::
268
282
269
283
except E as N:
270
284
foo
@@ -278,15 +292,17 @@ was translated to ::
278
292
del N
279
293
280
294
This means the exception must be assigned to a different name to be able to
281
- refer to it after the except clause. Exceptions are cleared because with the
295
+ refer to it after the :keyword: `!except ` clause.
296
+ Exceptions are cleared because with the
282
297
traceback attached to them, they form a reference cycle with the stack frame,
283
298
keeping all locals in that frame alive until the next garbage collection occurs.
284
299
285
300
.. index ::
286
301
module: sys
287
302
object: traceback
288
303
289
- Before an except clause's suite is executed, details about the exception are
304
+ Before an :keyword: `!except ` clause's suite is executed,
305
+ details about the exception are
290
306
stored in the :mod: `sys ` module and can be accessed via :func: `sys.exc_info `.
291
307
:func: `sys.exc_info ` returns a 3-tuple consisting of the exception class, the
292
308
exception instance and a traceback object (see section :ref: `types `) identifying
@@ -312,17 +328,24 @@ when leaving an exception handler::
312
328
>>> print(sys.exc_info())
313
329
(None, None, None)
314
330
331
+
315
332
.. index ::
316
333
keyword: except_star
317
334
318
- The :keyword: `except*<except_star> ` clause(s) are used for handling
319
- :exc: `ExceptionGroup `\ s. The exception type for matching is interpreted as in
335
+ .. _except_star :
336
+
337
+ :keyword: `!except* ` clause
338
+ --------------------------
339
+
340
+ The :keyword: `!except* ` clause(s) are used for handling
341
+ :exc: `ExceptionGroup `\s . The exception type for matching is interpreted as in
320
342
the case of :keyword: `except `, but in the case of exception groups we can have
321
343
partial matches when the type matches some of the exceptions in the group.
322
- This means that multiple except* clauses can execute, each handling part of
323
- the exception group. Each clause executes once and handles an exception group
344
+ This means that multiple :keyword: `!except* ` clauses can execute,
345
+ each handling part of the exception group.
346
+ Each clause executes once and handles an exception group
324
347
of all matching exceptions. Each exception in the group is handled by at most
325
- one except* clause, the first that matches it. ::
348
+ one :keyword: ` ! except*` clause, the first that matches it. ::
326
349
327
350
>>> try:
328
351
... raise ExceptionGroup("eg",
@@ -341,16 +364,16 @@ one except* clause, the first that matches it. ::
341
364
| ValueError: 1
342
365
+------------------------------------
343
366
344
- Any remaining exceptions that were not handled by any :keyword: `!except* `
345
- clause are re-raised at the end, combined into an exception group along with
346
- all exceptions that were raised from within :keyword: `!except* ` clauses.
367
+ Any remaining exceptions that were not handled by any :keyword:`!except*`
368
+ clause are re-raised at the end, combined into an exception group along with
369
+ all exceptions that were raised from within :keyword:`!except*` clauses.
347
370
348
- An :keyword: `!except* ` clause must have a matching type,
349
- and this type cannot be a subclass of :exc: `BaseExceptionGroup `.
350
- It is not possible to mix :keyword: `except ` and :keyword: `!except* `
351
- in the same :keyword: `try `.
352
- :keyword: `break `, :keyword: `continue ` and :keyword: `return `
353
- cannot appear in an :keyword: `!except* ` clause.
371
+ An :keyword:`!except*` clause must have a matching type,
372
+ and this type cannot be a subclass of :exc:`BaseExceptionGroup`.
373
+ It is not possible to mix :keyword:`except` and :keyword:`!except*`
374
+ in the same :keyword:`try`.
375
+ :keyword:`break`, :keyword:`continue` and :keyword:`return`
376
+ cannot appear in an :keyword:`!except*` clause.
354
377
355
378
356
379
.. index ::
@@ -359,17 +382,28 @@ cannot appear in an :keyword:`!except*` clause.
359
382
statement: break
360
383
statement: continue
361
384
385
+ .. _except_else :
386
+
387
+ :keyword: `!else ` clause
388
+ -----------------------
389
+
362
390
The optional :keyword: `!else ` clause is executed if the control flow leaves the
363
391
:keyword: `try ` suite, no exception was raised, and no :keyword: `return `,
364
392
:keyword: `continue `, or :keyword: `break ` statement was executed. Exceptions in
365
393
the :keyword: `!else ` clause are not handled by the preceding :keyword: `except `
366
394
clauses.
367
395
396
+
368
397
.. index :: keyword: finally
369
398
370
- If :keyword: `finally ` is present, it specifies a 'cleanup' handler. The
399
+ .. _finally :
400
+
401
+ :keyword: `!finally ` clause
402
+ --------------------------
403
+
404
+ If :keyword: `!finally ` is present, it specifies a 'cleanup' handler. The
371
405
:keyword: `try ` clause is executed, including any :keyword: `except ` and
372
- :keyword: `! else ` clauses. If an exception occurs in any of the clauses and is
406
+ :keyword: `else ` clauses. If an exception occurs in any of the clauses and is
373
407
not handled, the exception is temporarily saved. The :keyword: `!finally ` clause
374
408
is executed. If there is a saved exception it is re-raised at the end of the
375
409
:keyword: `!finally ` clause. If the :keyword: `!finally ` clause raises another
@@ -387,7 +421,7 @@ or :keyword:`continue` statement, the saved exception is discarded::
387
421
42
388
422
389
423
The exception information is not available to the program during execution of
390
- the :keyword: `finally ` clause.
424
+ the :keyword: `! finally ` clause.
391
425
392
426
.. index ::
393
427
statement: return
@@ -396,10 +430,10 @@ the :keyword:`finally` clause.
396
430
397
431
When a :keyword: `return `, :keyword: `break ` or :keyword: `continue ` statement is
398
432
executed in the :keyword: `try ` suite of a :keyword: `!try `...\ :keyword: `!finally `
399
- statement, the :keyword: `finally ` clause is also executed 'on the way out.'
433
+ statement, the :keyword: `! finally ` clause is also executed 'on the way out.'
400
434
401
435
The return value of a function is determined by the last :keyword: `return `
402
- statement executed. Since the :keyword: `finally ` clause always executes, a
436
+ statement executed. Since the :keyword: `! finally ` clause always executes, a
403
437
:keyword: `!return ` statement executed in the :keyword: `!finally ` clause will
404
438
always be the last one executed::
405
439
@@ -412,13 +446,9 @@ always be the last one executed::
412
446
>>> foo()
413
447
'finally'
414
448
415
- Additional information on exceptions can be found in section :ref: `exceptions `,
416
- and information on using the :keyword: `raise ` statement to generate exceptions
417
- may be found in section :ref: `raise `.
418
-
419
449
.. versionchanged :: 3.8
420
450
Prior to Python 3.8, a :keyword: `continue ` statement was illegal in the
421
- :keyword: `finally ` clause due to a problem with the implementation.
451
+ :keyword: `! finally ` clause due to a problem with the implementation.
422
452
423
453
424
454
.. _with :
0 commit comments