Error Handling 1

Extension modules indicate errors by returning NULL to the interpreter. Prior to returning NULL, an exception should be set using one of the following functions:

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 file name 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

C Name

Python Exception

PyExc_

_OSError

OSError

PyExc_

OverflowError

OverflowError

PyExc_

ReferenceError

ReferenceError

PyExc_

RuntimeError

RuntimeError

PyExc_

StandardError

StandardError

PyExc_

StopIteration

StopIteration

PyExc_

SyntaxError

SyntaxError

PyExc_

SystemError

SystemError

PyExc_

SystemExit

SystemExit

PyExc_

TypeError

TypeError

PyExc_

UnicodeError

UnicodeError

PyExc_

UnicodeEncodeError

UnicodeEncodeError

PyExc_

UnicodeDecodeError

UnicodeDecodeError

PyExc_

UnicodeTranslateError

UnicodeTranslateError

PyExc_

ValueError

ValueError

PyExc_

WindowsError

WindowsError

PyExc_

ZeroDivisionError

ZeroDivisionError

The following functions are used to query or clear the exception status of the interpreter:

void PyErr_Clear()

Clears any previously raised exceptions. PyObject *PyErr_Occurred()

Checks to see whether or not an exception has been raised. If so, the current exception value is returned. Otherwise, NULL is returned.

int PyErr_ExceptionMatches(PyObject *exc)

Checks to see if the current exception matches the exception exc. Returns 1 if true, 0 otherwise. This function follows the same exception matching rules as in Python code. Therefore, exc could be a superclass of the current exception or a tuple of exception classes.

The following prototype shows how to implement a try-except block in C:

/* Carry out some operation involving Python objects */ if (PyErr_Occurred()) {

if (PyErr_ExceptionMatches(PyExc_ValueError)) { /* Take some kind of recovery action */

PyErr_Clear();

return NULL; /* Propagate the exception to the interpreter */

0 0

Post a comment

  • Receive news updates via email from this site