@@ -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",
@@ -342,15 +365,16 @@ one except* clause, the first that matches it. ::
342
365
+------------------------------------
343
366
>>>
344
367
345
- Any remaining exceptions that were not handled by any except* clause
346
- are re-raised at the end, combined into an exception group along with
347
- all exceptions that were raised from within except* clauses.
368
+ Any remaining exceptions that were not handled by any :keyword:`! except*`
369
+ clause are re-raised at the end, combined into an exception group along with
370
+ all exceptions that were raised from within :keyword:`! except*` clauses.
348
371
349
- An except* clause must have a matching type, and this type cannot be a
350
- subclass of :exc:`BaseExceptionGroup`. It is not possible to mix except
351
- and except* in the same :keyword:`try`. :keyword:`break`,
352
- :keyword:`continue` and :keyword:`return` cannot appear in an except*
353
- clause.
372
+ An :keyword:`!except*` clause must have a matching type,
373
+ and this type cannot be a subclass of :exc:`BaseExceptionGroup`.
374
+ It is not possible to mix :keyword:`except` and :keyword:`!except*`
375
+ in the same :keyword:`try`.
376
+ :keyword:`break`, :keyword:`continue` and :keyword:`return`
377
+ cannot appear in an :keyword:`!except*` clause.
354
378
355
379
356
380
.. index ::
@@ -359,17 +383,28 @@ one except* clause, the first that matches it. ::
359
383
statement: break
360
384
statement: continue
361
385
386
+ .. _except_else :
387
+
388
+ :keyword: `!else ` clause
389
+ -----------------------
390
+
362
391
The optional :keyword: `!else ` clause is executed if the control flow leaves the
363
392
:keyword: `try ` suite, no exception was raised, and no :keyword: `return `,
364
393
:keyword: `continue `, or :keyword: `break ` statement was executed. Exceptions in
365
394
the :keyword: `!else ` clause are not handled by the preceding :keyword: `except `
366
395
clauses.
367
396
397
+
368
398
.. index :: keyword: finally
369
399
370
- If :keyword: `finally ` is present, it specifies a 'cleanup' handler. The
400
+ .. _finally :
401
+
402
+ :keyword: `!finally ` clause
403
+ --------------------------
404
+
405
+ If :keyword: `!finally ` is present, it specifies a 'cleanup' handler. The
371
406
: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
407
+ :keyword: `else ` clauses. If an exception occurs in any of the clauses and is
373
408
not handled, the exception is temporarily saved. The :keyword: `!finally ` clause
374
409
is executed. If there is a saved exception it is re-raised at the end of the
375
410
:keyword: `!finally ` clause. If the :keyword: `!finally ` clause raises another
@@ -387,7 +422,7 @@ or :keyword:`continue` statement, the saved exception is discarded::
387
422
42
388
423
389
424
The exception information is not available to the program during execution of
390
- the :keyword: `finally ` clause.
425
+ the :keyword: `! finally ` clause.
391
426
392
427
.. index ::
393
428
statement: return
@@ -396,10 +431,10 @@ the :keyword:`finally` clause.
396
431
397
432
When a :keyword: `return `, :keyword: `break ` or :keyword: `continue ` statement is
398
433
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.'
434
+ statement, the :keyword: `! finally ` clause is also executed 'on the way out.'
400
435
401
436
The return value of a function is determined by the last :keyword: `return `
402
- statement executed. Since the :keyword: `finally ` clause always executes, a
437
+ statement executed. Since the :keyword: `! finally ` clause always executes, a
403
438
:keyword: `!return ` statement executed in the :keyword: `!finally ` clause will
404
439
always be the last one executed::
405
440
@@ -412,13 +447,9 @@ always be the last one executed::
412
447
>>> foo()
413
448
'finally'
414
449
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
450
.. versionchanged :: 3.8
420
451
Prior to Python 3.8, a :keyword: `continue ` statement was illegal in the
421
- :keyword: `finally ` clause due to a problem with the implementation.
452
+ :keyword: `! finally ` clause due to a problem with the implementation.
422
453
423
454
424
455
.. _with :
0 commit comments