@@ -45,14 +45,20 @@ programs, however, and result in error messages as shown here::
4545 >>> 10 * (1/0)
4646 Traceback (most recent call last):
4747 File "<stdin>", line 1, in <module>
48+ 10 * (1/0)
49+ ~^~
4850 ZeroDivisionError: division by zero
4951 >>> 4 + spam*3
5052 Traceback (most recent call last):
5153 File "<stdin>", line 1, in <module>
54+ 4 + spam*3
55+ ^^^^
5256 NameError: name 'spam' is not defined
5357 >>> '2' + 2
5458 Traceback (most recent call last):
5559 File "<stdin>", line 1, in <module>
60+ '2' + 2
61+ ~~~~^~~
5662 TypeError: can only concatenate str (not "int") to str
5763
5864The last line of the error message indicates what happened. Exceptions come in
@@ -252,6 +258,7 @@ exception to occur. For example::
252258 >>> raise NameError('HiThere')
253259 Traceback (most recent call last):
254260 File "<stdin>", line 1, in <module>
261+ raise NameError('HiThere')
255262 NameError: HiThere
256263
257264The sole argument to :keyword: `raise ` indicates the exception to be raised.
@@ -275,6 +282,7 @@ re-raise the exception::
275282 An exception flew by!
276283 Traceback (most recent call last):
277284 File "<stdin>", line 2, in <module>
285+ raise NameError('HiThere')
278286 NameError: HiThere
279287
280288
@@ -294,12 +302,15 @@ message::
294302 ...
295303 Traceback (most recent call last):
296304 File "<stdin>", line 2, in <module>
305+ open("database.sqlite")
306+ ~~~~^^^^^^^^^^^^^^^^^^^
297307 FileNotFoundError: [Errno 2] No such file or directory: 'database.sqlite'
298308 <BLANKLINE>
299309 During handling of the above exception, another exception occurred:
300310 <BLANKLINE>
301311 Traceback (most recent call last):
302312 File "<stdin>", line 4, in <module>
313+ raise RuntimeError("unable to handle error")
303314 RuntimeError: unable to handle error
304315
305316To 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::
320331 ...
321332 Traceback (most recent call last):
322333 File "<stdin>", line 2, in <module>
334+ func()
335+ ~~~~^^
323336 File "<stdin>", line 2, in func
324337 ConnectionError
325338 <BLANKLINE>
326339 The above exception was the direct cause of the following exception:
327340 <BLANKLINE>
328341 Traceback (most recent call last):
329342 File "<stdin>", line 4, in <module>
343+ raise RuntimeError('Failed to open database') from exc
330344 RuntimeError: Failed to open database
331345
332346It also allows disabling automatic exception chaining using the ``from None ``
@@ -339,6 +353,7 @@ idiom::
339353 ...
340354 Traceback (most recent call last):
341355 File "<stdin>", line 4, in <module>
356+ raise RuntimeError from None
342357 RuntimeError
343358
344359For more information about chaining mechanics, see :ref: `bltin-exceptions `.
@@ -381,6 +396,7 @@ example::
381396 Goodbye, world!
382397 Traceback (most recent call last):
383398 File "<stdin>", line 2, in <module>
399+ raise KeyboardInterrupt
384400 KeyboardInterrupt
385401
386402If a :keyword: `finally ` clause is present, the :keyword: `!finally `
@@ -448,7 +464,11 @@ A more complicated example::
448464 executing finally clause
449465 Traceback (most recent call last):
450466 File "<stdin>", line 1, in <module>
467+ divide("2", "0")
468+ ~~~~~~^^^^^^^^^^
451469 File "<stdin>", line 3, in divide
470+ result = x / y
471+ ~~^~~
452472 TypeError: unsupported operand type(s) for /: 'str' and 'str'
453473
454474As you can see, the :keyword: `finally ` clause is executed in any event. The
@@ -511,8 +531,11 @@ caught like any other exception. ::
511531 >>> f()
512532 + Exception Group Traceback (most recent call last):
513533 | File "<stdin>", line 1, in <module>
534+ | f()
535+ | ~^^
514536 | 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)
516539 +-+---------------- 1 ----------------
517540 | OSError: error 1
518541 +---------------- 2 ----------------
@@ -560,10 +583,15 @@ other clauses and eventually to be reraised. ::
560583 There were SystemErrors
561584 + Exception Group Traceback (most recent call last):
562585 | File "<stdin>", line 2, in <module>
586+ | f()
587+ | ~^^
563588 | File "<stdin>", line 2, in f
564- | ExceptionGroup: group1
589+ | raise ExceptionGroup(
590+ | ...<12 lines>...
591+ | )
592+ | ExceptionGroup: group1 (1 sub-exception)
565593 +-+---------------- 1 ----------------
566- | ExceptionGroup: group2
594+ | ExceptionGroup: group2 (1 sub-exception)
567595 +-+---------------- 1 ----------------
568596 | RecursionError: 4
569597 +------------------------------------
@@ -607,6 +635,7 @@ includes all notes, in the order they were added, after the exception. ::
607635 ...
608636 Traceback (most recent call last):
609637 File "<stdin>", line 2, in <module>
638+ raise TypeError('bad type')
610639 TypeError: bad type
611640 Add some information
612641 Add some more information
@@ -630,23 +659,33 @@ exception in the group has a note indicating when this error has occurred. ::
630659 >>> raise ExceptionGroup('We have some problems', excs)
631660 + Exception Group Traceback (most recent call last):
632661 | File "<stdin>", line 1, in <module>
662+ | raise ExceptionGroup('We have some problems', excs)
633663 | ExceptionGroup: We have some problems (3 sub-exceptions)
634664 +-+---------------- 1 ----------------
635665 | Traceback (most recent call last):
636666 | File "<stdin>", line 3, in <module>
667+ | f()
668+ | ~^^
637669 | File "<stdin>", line 2, in f
670+ | raise OSError('operation failed')
638671 | OSError: operation failed
639672 | Happened in Iteration 1
640673 +---------------- 2 ----------------
641674 | Traceback (most recent call last):
642675 | File "<stdin>", line 3, in <module>
676+ | f()
677+ | ~^^
643678 | File "<stdin>", line 2, in f
679+ | raise OSError('operation failed')
644680 | OSError: operation failed
645681 | Happened in Iteration 2
646682 +---------------- 3 ----------------
647683 | Traceback (most recent call last):
648684 | File "<stdin>", line 3, in <module>
685+ | f()
686+ | ~^^
649687 | File "<stdin>", line 2, in f
688+ | raise OSError('operation failed')
650689 | OSError: operation failed
651690 | Happened in Iteration 3
652691 +------------------------------------
0 commit comments