Linux/Unix > Python Essential Reference, Second Edition > B. Extending and Embedding Python > Error Handling | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105095080114204070017095

### Error Handling

Errors are indicated by returning `NULL` to the interpreter. Prior to returning `NULL`, an exception should be set or cleared using one of the following functions:

    void PyErr_Clear()

Clears any previously raised exceptions.

    PyObject *PyErr_Occurred()

Checks to see whether an error has been generated. If so, returns the current exception object. Otherwise, returns `NULL`.

    void PyErr_NoMemory()

Raises a `MemoryError` exception.

    void PyErr_SetFromErrno(PyObject *exc)

Raises an exception. `exc` is an exception object. The value of the exception is taken from the `errno` variable in the C library.

    void PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)

Like `PyErr_SetFromErrno()` but includes the filename in the exception value as well.

    void PyErr_SetObject(PyObject *exc, PyObject *val)

Raises an exception. `exc` is an exception object and `val` is an object containing the value of the exception.

    void PyErr_SetString(PyObject *exc, char *msg)

Raises an exception. `exc` is an exception object and `msg` is a message describing what went wrong.

The `exc` argument in these functions can be set to one of the following:

C Name | Python Exception
--- | ---
`PyExc_ArithmeticError` | `ArithmeticError`
`PyExc_AssertionError` | `AssertionError`
`PyExc_AttributeError` | `AttributeError`
`PyExc_EnvironmentError` | `EnvironmentError`
`PyExc_EOFError` | `EOFError`
`PyExc_Exception` | `Exception`
`PyExc_FloatingPointError` | `FloatingPointError`
`PyExc_ImportError` | `ImportError`
`PyExc_IndexError` | `IndexError`
`PyExc_IOError` | `IOError`
`PyExc_KeyError` | `KeyError`
`PyExc_KeyboardInterrupt` | `KeyboardInterrupt`
`PyExc_LookupError` | `LookupError`
`PyExc_MemoryError` | `MemoryError`
`PyExc_NameError` | `NameError`
`PyExc_NotImplementedError` | `NotImplementedError`
`PyExc_OSError` | `OSError`
`PyExc_OverflowError` | `OverflowError`
`PyExc_RuntimeError` | `RuntimeError`
`PyExc_StandardError` | `StandardError`
`PyExc_SyntaxError` | `SyntaxError`
`PyExc_SystemError` | `SystemError`
`PyExc_SystemExit` | `SystemExit`
`PyExc_TypeError` | `TypeError`
`PyExc_UnicodeError` | `UnicodeError`
`PyExc_ValueError` | `ValueError`
`PyExc_ZeroDivisionError` | `ZeroDivisionError`

The following example shows how an exception is typically set and an error returned in extension code:

    PyErr_SetString(PyExc_ValueError,"Expected a positive value!");
    return NULL;

An extension module can define a new exception type by using the following function:

    PyObject *PyErr_NewException(char *excname, PyObject *base, PyObject *dict)

Creates a new exception object. `excname` is the name of the exception in the form `"``modulename` `.``excname` `"`, `base` is an optional base class for the exception, and `dict` is an optional dictionary used as the `_ _dict_ _` attribute of the resulting exception class. Both of these arguments are normally set to `NULL`. The returned object is a class object.

The following example shows how a new exception is created in an extension module:

    static PyObject *SpamError;
    ...

    /* Module initialization function */
    initspam(void) {
         PyObject *m, *d;
         m = Py_InitModule("spam",SpamMethods);
         d = PyModule_GetDict(m);
         SpamError = PyErr_NewException("spam.error", NULL, NULL);
         PyDict_SetItemString(d,"error",SpamError);
         ...
    }

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.