@@ -45,14 +45,20 @@ programs, however, and result in error messages as shown here::
45
45
>>> 10 * (1/0)
46
46
Traceback (most recent call last):
47
47
File "<stdin>", line 1, in <module>
48
+ 10 * (1/0)
49
+ ~^~
48
50
ZeroDivisionError: division by zero
49
51
>>> 4 + spam*3
50
52
Traceback (most recent call last):
51
53
File "<stdin>", line 1, in <module>
54
+ 4 + spam*3
55
+ ^^^^
52
56
NameError: name 'spam' is not defined
53
57
>>> '2' + 2
54
58
Traceback (most recent call last):
55
59
File "<stdin>", line 1, in <module>
60
+ '2' + 2
61
+ ~~~~^~~
56
62
TypeError: can only concatenate str (not "int") to str
57
63
58
64
The last line of the error message indicates what happened. Exceptions come in
@@ -252,6 +258,7 @@ exception to occur. For example::
252
258
>>> raise NameError('HiThere')
253
259
Traceback (most recent call last):
254
260
File "<stdin>", line 1, in <module>
261
+ raise NameError('HiThere')
255
262
NameError: HiThere
256
263
257
264
The sole argument to :keyword: `raise ` indicates the exception to be raised.
@@ -275,6 +282,7 @@ re-raise the exception::
275
282
An exception flew by!
276
283
Traceback (most recent call last):
277
284
File "<stdin>", line 2, in <module>
285
+ raise NameError('HiThere')
278
286
NameError: HiThere
279
287
280
288
@@ -294,12 +302,15 @@ message::
294
302
...
295
303
Traceback (most recent call last):
296
304
File "<stdin>", line 2, in <module>
305
+ open("database.sqlite")
306
+ ~~~~^^^^^^^^^^^^^^^^^^^
297
307
FileNotFoundError: [Errno 2] No such file or directory: 'database.sqlite'
298
308
<BLANKLINE>
299
309
During handling of the above exception, another exception occurred:
300
310
<BLANKLINE>
301
311
Traceback (most recent call last):
302
312
File "<stdin>", line 4, in <module>
313
+ raise RuntimeError("unable to handle error")
303
314
RuntimeError: unable to handle error
304
315
305
316
To indicate that an exception is a direct consequence of another, the
@@ -320,13 +331,16 @@ This can be useful when you are transforming exceptions. For example::
320
331
...
321
332
Traceback (most recent call last):
322
333
File "<stdin>", line 2, in <module>
334
+ func()
335
+ ~~~~^^
323
336
File "<stdin>", line 2, in func
324
337
ConnectionError
325
338
<BLANKLINE>
326
339
The above exception was the direct cause of the following exception:
327
340
<BLANKLINE>
328
341
Traceback (most recent call last):
329
342
File "<stdin>", line 4, in <module>
343
+ raise RuntimeError('Failed to open database') from exc
330
344
RuntimeError: Failed to open database
331
345
332
346
It also allows disabling automatic exception chaining using the ``from None ``
@@ -339,6 +353,7 @@ idiom::
339
353
...
340
354
Traceback (most recent call last):
341
355
File "<stdin>", line 4, in <module>
356
+ raise RuntimeError from None
342
357
RuntimeError
343
358
344
359
For more information about chaining mechanics, see :ref: `bltin-exceptions `.
@@ -381,6 +396,7 @@ example::
381
396
Goodbye, world!
382
397
Traceback (most recent call last):
383
398
File "<stdin>", line 2, in <module>
399
+ raise KeyboardInterrupt
384
400
KeyboardInterrupt
385
401
386
402
If a :keyword: `finally ` clause is present, the :keyword: `!finally `
@@ -448,7 +464,11 @@ A more complicated example::
448
464
executing finally clause
449
465
Traceback (most recent call last):
450
466
File "<stdin>", line 1, in <module>
467
+ divide("2", "0")
468
+ ~~~~~~^^^^^^^^^^
451
469
File "<stdin>", line 3, in divide
470
+ result = x / y
471
+ ~~^~~
452
472
TypeError: unsupported operand type(s) for /: 'str' and 'str'
453
473
454
474
As you can see, the :keyword: `finally ` clause is executed in any event. The
@@ -511,8 +531,11 @@ caught like any other exception. ::
511
531
>>> f()
512
532
+ Exception Group Traceback (most recent call last):
513
533
| File "<stdin>", line 1, in <module>
534
+ | f()
535
+ | ~^^
514
536
| File "<stdin>", line 3, in f
515
- | ExceptionGroup: there were problems
537
+ | raise ExceptionGroup('there were problems', excs)
538
+ | ExceptionGroup: there were problems (2 sub-exceptions)
516
539
+-+---------------- 1 ----------------
517
540
| OSError: error 1
518
541
+---------------- 2 ----------------
@@ -560,10 +583,15 @@ other clauses and eventually to be reraised. ::
560
583
There were SystemErrors
561
584
+ Exception Group Traceback (most recent call last):
562
585
| File "<stdin>", line 2, in <module>
586
+ | f()
587
+ | ~^^
563
588
| File "<stdin>", line 2, in f
564
- | ExceptionGroup: group1
589
+ | raise ExceptionGroup(
590
+ | ...<12 lines>...
591
+ | )
592
+ | ExceptionGroup: group1 (1 sub-exception)
565
593
+-+---------------- 1 ----------------
566
- | ExceptionGroup: group2
594
+ | ExceptionGroup: group2 (1 sub-exception)
567
595
+-+---------------- 1 ----------------
568
596
| RecursionError: 4
569
597
+------------------------------------
@@ -607,6 +635,7 @@ includes all notes, in the order they were added, after the exception. ::
607
635
...
608
636
Traceback (most recent call last):
609
637
File "<stdin>", line 2, in <module>
638
+ raise TypeError('bad type')
610
639
TypeError: bad type
611
640
Add some information
612
641
Add some more information
@@ -630,23 +659,33 @@ exception in the group has a note indicating when this error has occurred. ::
630
659
>>> raise ExceptionGroup('We have some problems', excs)
631
660
+ Exception Group Traceback (most recent call last):
632
661
| File "<stdin>", line 1, in <module>
662
+ | raise ExceptionGroup('We have some problems', excs)
633
663
| ExceptionGroup: We have some problems (3 sub-exceptions)
634
664
+-+---------------- 1 ----------------
635
665
| Traceback (most recent call last):
636
666
| File "<stdin>", line 3, in <module>
667
+ | f()
668
+ | ~^^
637
669
| File "<stdin>", line 2, in f
670
+ | raise OSError('operation failed')
638
671
| OSError: operation failed
639
672
| Happened in Iteration 1
640
673
+---------------- 2 ----------------
641
674
| Traceback (most recent call last):
642
675
| File "<stdin>", line 3, in <module>
676
+ | f()
677
+ | ~^^
643
678
| File "<stdin>", line 2, in f
679
+ | raise OSError('operation failed')
644
680
| OSError: operation failed
645
681
| Happened in Iteration 2
646
682
+---------------- 3 ----------------
647
683
| Traceback (most recent call last):
648
684
| File "<stdin>", line 3, in <module>
685
+ | f()
686
+ | ~^^
649
687
| File "<stdin>", line 2, in f
688
+ | raise OSError('operation failed')
650
689
| OSError: operation failed
651
690
| Happened in Iteration 3
652
691
+------------------------------------
0 commit comments