@@ -34,6 +34,10 @@ class or one of its subclasses, and not from :exc:`BaseException`. More
34
34
information on defining exceptions is available in the Python Tutorial under
35
35
:ref: `tut-userexceptions `.
36
36
37
+
38
+ Exception context
39
+ -----------------
40
+
37
41
When raising (or re-raising) an exception in an :keyword: `except ` or
38
42
:keyword: `finally ` clause
39
43
:attr: `__context__ ` is automatically set to the last exception caught; if the
@@ -67,6 +71,25 @@ exceptions so that the final line of the traceback always shows the last
67
71
exception that was raised.
68
72
69
73
74
+ Inheriting from built-in exceptions
75
+ -----------------------------------
76
+
77
+ User code can create subclasses that inherit from an exception type.
78
+ It's recommended to only subclass one exception type at a time to avoid
79
+ any possible conflicts between how the bases handle the ``args ``
80
+ attribute, as well as due to possible memory layout incompatibilities.
81
+
82
+ .. impl-detail ::
83
+
84
+ Most built-in exceptions are implemented in C for efficiency, see:
85
+ :source: `Objects/exceptions.c `. Some have custom memory layouts
86
+ which makes it impossible to create a subclass that inherits from
87
+ multiple exception types. The memory layout of a type is an implementation
88
+ detail and might change between Python versions, leading to new
89
+ conflicts in the future. Therefore, it's recommended to avoid
90
+ subclassing multiple exception types altogether.
91
+
92
+
70
93
Base classes
71
94
------------
72
95
0 commit comments