@@ -25,6 +25,8 @@ These CPython calls are the most useful:
25
25
`PyErr_Occurred() documentation <https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Occurred >`_
26
26
* ``PyErr_Clear() `` - Clearing any set exceptions, have good reason to do this!
27
27
`PyErr_Clear() documentation <https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Clear >`_
28
+ * ``PyErr_Print() `` - Print a representation of the current exception then clear any set exceptions.
29
+ `PyErr_Print() documentation <https://docs.python.org/3.13/c-api/exceptions.html#c.PyErr_Print >`_
28
30
29
31
Indicating an error condition is a two stage process; your code must register an exception and then indicate failure
30
32
by returning ``NULL ``. Here is a C function doing just that:
@@ -169,9 +171,10 @@ The following C code is equivalent to the Python code:
169
171
class SpecialisedError (ExceptionBase ):
170
172
pass
171
173
172
- This can be done quite easily using either the ``PyErr_NewException `` or the ``PyErr_NewExceptionWithDoc `` functions.
173
- These create new exception classes that can be added to a module.
174
- For example:
174
+ Declaring Specialised Exceptions
175
+ ---------------------------------
176
+
177
+ Firstly declare the ``PyObject * `` exception:
175
178
176
179
.. code-block :: c
177
180
@@ -182,6 +185,11 @@ For example:
182
185
183
186
/* NOTE: Functions that might raise one of these exceptions will go here. See below. */
184
187
188
+
189
+
190
+ Example Module
191
+ -----------------------------------
192
+
185
193
Now define the module, ``cExceptions_methods `` is explained later:
186
194
187
195
.. code-block :: c
@@ -195,7 +203,12 @@ Now define the module, ``cExceptions_methods`` is explained later:
195
203
NULL, NULL, NULL, NULL,
196
204
};
197
205
198
- Initialise the module, this registers the exception types and the class hierarchy:
206
+ Initialising Specialised Exceptions
207
+ -----------------------------------
208
+
209
+ This can be done quite easily using either the ``PyErr_NewException `` or the ``PyErr_NewExceptionWithDoc `` functions.
210
+ These create new exception classes that can be added to a module.
211
+ For example, initialise the module, this registers the exception types and the class hierarchy:
199
212
200
213
.. code-block :: c
201
214
@@ -246,6 +259,9 @@ Initialise the module, this registers the exception types and the class hierarch
246
259
return m;
247
260
}
248
261
262
+ Raising Specialise Exceptions
263
+ -----------------------------
264
+
249
265
To illustrate how you raise one of these exceptions suppose we have a function to test raising one of these exceptions:
250
266
251
267
.. code-block :: c
0 commit comments