例外處理¶
The functions described in this chapter will let you handle and raise Python
exceptions. It is important to understand some of the basics of Python
exception handling. It works somewhat like the POSIX errno variable:
there is a global indicator (per thread) of the last error that occurred. Most
C API functions don’t clear this on success, but will set it to indicate the
cause of the error on failure. Most C API functions also return an error
indicator, usually NULL if they are supposed to return a pointer, or -1
if they return an integer (exception: the PyArg_*() functions
return 1 for success and 0 for failure).
Concretely, the error indicator consists of three object pointers: the exception’s type, the exception’s value, and the traceback object. Any of those pointers can be NULL if non-set (although some combinations are forbidden, for example you can’t have a non-NULL traceback if the exception type is NULL).
When a function must fail because some function it called failed, it generally doesn’t set the error indicator; the function it called already set it. It is responsible for either handling the error and clearing the exception or returning after cleaning up any resources it holds (such as object references or memory allocations); it should not continue normally if it is not prepared to handle the error. If returning due to an error, it is important to indicate to the caller that an error has been set. If the error is not handled or carefully propagated, additional calls into the Python/C API may not behave as intended and may fail in mysterious ways.
備註
The error indicator is not the result of sys.exc_info().
The former corresponds to an exception that is not yet caught (and is
therefore still propagating), while the latter returns an exception after
it is caught (and has therefore stopped propagating).
Printing and clearing¶
-
void
PyErr_Clear()¶ Clear the error indicator. If the error indicator is not set, there is no effect.
-
void
PyErr_PrintEx(int set_sys_last_vars)¶ Print a standard traceback to
sys.stderrand clear the error indicator. Call this function only when the error indicator is set. (Otherwise it will cause a fatal error!)If set_sys_last_vars is nonzero, the variables
sys.last_type,sys.last_valueandsys.last_tracebackwill be set to the type, value and traceback of the printed exception, respectively.
-
void
PyErr_Print()¶ Alias for
PyErr_PrintEx(1).
-
void
PyErr_WriteUnraisable(PyObject *obj)¶ This utility function prints a warning message to
sys.stderrwhen an exception has been set but it is impossible for the interpreter to actually raise the exception. It is used, for example, when an exception occurs in an__del__()method.The function is called with a single argument obj that identifies the context in which the unraisable exception occurred. If possible, the repr of obj will be printed in the warning message.
Raising exceptions¶
These functions help you set the current thread’s error indicator.
For convenience, some of these functions will always return a
NULL pointer for use in a return statement.
-
void
PyErr_SetString(PyObject *type, const char *message)¶ This is the most common way to set the error indicator. The first argument specifies the exception type; it is normally one of the standard exceptions, e.g.
PyExc_RuntimeError. You need not increment its reference count. The second argument is an error message; it is decoded from'utf-8』.
-
void
PyErr_SetObject(PyObject *type, PyObject *value)¶ This function is similar to
PyErr_SetString()but lets you specify an arbitrary Python object for the 「value」 of the exception.
-
PyObject*
PyErr_Format(PyObject *exception, const char *format, ...)¶ - Return value: Always NULL.
This function sets the error indicator and returns NULL. exception should be a Python exception class. The format and subsequent parameters help format the error message; they have the same meaning and values as in
PyUnicode_FromFormat(). format is an ASCII-encoded string.
-
PyObject*
PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)¶ - Return value: Always NULL.
Same as
PyErr_Format(), but taking ava_listargument rather than a variable number of arguments.3.5 版新加入.
-
void
PyErr_SetNone(
