@@ -307,13 +307,13 @@ try:
307
307
)
308
308
)
309
309
except * TypeError as e:
310
- print (f ' got some TypeErrors: { len (e)} ' )
310
+ print (f ' got some TypeErrors: { list (e)} ' )
311
311
except * Exception :
312
312
pass
313
313
314
314
# would print:
315
315
#
316
- # got some TypeErrors: 2
316
+ # got some TypeErrors: [TypeError('b'), TypeError('c')]
317
317
```
318
318
319
319
Iteration over an ` ExceptionGroup ` that has nested ` ExceptionGroup ` objects
@@ -338,9 +338,12 @@ print(
338
338
# [ValueError('a'), TypeError('b'), TypeError('c'), KeyError('d')]
339
339
```
340
340
341
- ### "raise e" and "raise"
341
+ ### Re-raising ExceptionGroups
342
342
343
- There is no difference between bare ` raise ` and a more specific ` raise e ` :
343
+ It is important to point out that the ` ExceptionGroup ` bound to ` e ` is an
344
+ ephemeral object. Raising it via ` raise ` or ` raise e ` will not cause changes
345
+ to the overall shape of the ` ExceptionGroup ` . Any modifications to it will
346
+ likely get lost:
344
347
345
348
``` python
346
349
try :
@@ -353,25 +356,75 @@ try:
353
356
)
354
357
)
355
358
except * TypeError as e:
356
- raise # or "raise e"
359
+ e.foo = ' bar'
360
+ # ^----------- `e` is an ephemeral object that might get
361
+ # destroyed after the `except*` clause.
362
+ ```
363
+
364
+ If the user wants to "flatten" the tree, they can explicitly create a new
365
+ ` ExceptionGroup ` and raise it:
366
+
367
+
368
+ ``` python
369
+ try :
370
+ raise ExceptionGroup(
371
+ ValueError (' a' ),
372
+ TypeError (' b' ),
373
+ ExceptionGroup(
374
+ TypeError (' c' ),
375
+ KeyError (' d' )
376
+ )
377
+ )
378
+ except * TypeError as e:
379
+ raise ExceptionGroup(* e)
357
380
358
381
# would terminate with:
359
382
#
360
383
# ExceptionGroup(
361
384
# ValueError('a'),
362
- # TypeError('b'),
363
385
# ExceptionGroup(
386
+ # TypeError('b'),
364
387
# TypeError('c'),
388
+ # ),
389
+ # ExceptionGroup(
365
390
# KeyError('d')
366
391
# )
367
392
# )
368
393
```
369
394
370
- It is important to point out that the ` ExceptionGroup ` bound to ` e ` is an
371
- ephemeral object. Raising it via ` raise ` or ` raise e ` will not cause changes
372
- to the overall shape of the ` ExceptionGroup ` . If the user wants to "flatten"
373
- the tree, they can explicitly create a new ` ExceptionGroup ` and raise it:
395
+ With the regular exceptions, there's a subtle difference between bare ` raise `
396
+ and a more specific ` raise e ` :
397
+
398
+ ``` python
399
+ def foo (): | def foo ():
400
+ try : | try :
401
+ 1 / 0 | 1 / 0
402
+ except ZeroDivisionError as e: | except ZeroDivisionError :
403
+ raise e | raise
404
+ |
405
+ foo() | foo()
406
+ |
407
+ Traceback (most recent call last): | Traceback (most recent call last):
408
+ File " /Users/guido/a.py" , line 7 | File " /Users/guido/b.py" , line 7
409
+ foo() | foo()
410
+ File " /Users/guido/a.py" , line 5 | File " /Users/guido/b.py" , line 3
411
+ raise e | 1 / 0
412
+ File " /Users/guido/a.py" , line 3 | ZeroDivisionError : division by zero
413
+ 1 / 0 |
414
+ ZeroDivisionError : division by zero |
415
+ ```
416
+
417
+ This difference is preserved with exception groups:
374
418
419
+ * The ` raise ` form re-raises all exceptions from the group * without recording
420
+ the current frame in their tracebacks* .
421
+
422
+ * The ` raise e ` form re-raises all exceptions from the group with tracebacks
423
+ updated to point out to the current frame, effectively resulting in user
424
+ seeing the ` raise e ` line in their tracebacks.
425
+
426
+ That said, both forms would not affect the overall shape of the exception
427
+ group:
375
428
376
429
``` python
377
430
try :
@@ -384,17 +437,15 @@ try:
384
437
)
385
438
)
386
439
except * TypeError as e:
387
- raise ExceptionGroup( * e)
440
+ raise # or "raise e"
388
441
389
- # would terminate with:
442
+ # would both terminate with:
390
443
#
391
444
# ExceptionGroup(
392
445
# ValueError('a'),
446
+ # TypeError('b'),
393
447
# ExceptionGroup(
394
- # TypeError('b'),
395
448
# TypeError('c'),
396
- # ),
397
- # ExceptionGroup(
398
449
# KeyError('d')
399
450
# )
400
451
# )
0 commit comments