Skip to main content

Overview

Exception handling in C extensions requires explicit checking of return values and managing the exception state.

Checking for Exceptions

Check if an exception is set. Returns: Exception type, or NULL if no exception

Setting Exceptions

PyErr_SetString

Example:

PyErr_Format

Example:

Standard Exceptions

Clearing Exceptions

Clear the exception state. Example:

Exception Matching

Check if current exception matches type.

Printing Exceptions

Print exception traceback to stderr.

Custom Exceptions

Example:

Error Handling Pattern

See Also