例外處理¶
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()¶
- 為 穩定 ABI 的一部分.
Clear the error indicator. If the error indicator is not set, there is no effect.
-
void PyErr_PrintEx(int set_sys_last_vars)¶
- 為 穩定 ABI 的一部分.
Print a standard traceback to
sys.stderrand clear the error indicator. Unless the error is aSystemExit, in that case no traceback is printed and the Python process will exit with the error code specified by theSystemExitinstance.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 variable
sys.last_excis set to the printed exception. For backwards compatibility, the deprecated variablessys.last_type,sys.last_valueandsys.last_tracebackare also set to the type, value and traceback of this exception, respectively.在 3.12 版的變更: 新增
sys.last_exc設定。
-
void PyErr_Print()¶
- 為 穩定 ABI 的一部分.
PyErr_PrintEx(1)的別名。
-
void PyErr_WriteUnraisable(PyObject *obj)¶
- 為 穩定 ABI 的一部分.
Call
sys.unraisablehook()using the current exception and obj argument.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. If obj is
NULL, only the traceback is printed.An exception must be set when calling this function.
在 3.4 版的變更: Print a traceback. Print only traceback if obj is
NULL.在 3.8 版的變更: 使用
sys.unraisablehook()。
-
void PyErr_FormatUnraisable(const char *format, ...)¶
Similar to
PyErr_WriteUnraisable(), but the format and subsequent parameters help format the warning message; they have the same meaning and values as inPyUnicode_FromFormat().PyErr_WriteUnraisable(obj)is roughly equivalent toPyErr_FormatUnraisable("Exception ignored in: %R", obj). If format isNULL, only the traceback is printed.在 3.13 版被加入.
-
void PyErr_DisplayException(PyObject *exc)¶
- 為 穩定 ABI 的一部分 自 3.12 版本開始.
Print the standard traceback display of
exctosys.stderr, including chained exceptions and notes.在 3.12 版被加入.
引發例外¶
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)