python抛出异常的作用,异常处理 - 抛出异常 - 《Python/C API 3.9.0 参考手册》 - 书栈网 · BookStack...

抛出异常

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(PyObjecttype*, const charmessage*)

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

This function is similar to PyErr_SetString() but lets you specify an arbitrary Python object for the “value” of the exception.

PyObject* PyErr_Format(PyObjectexception*, const charformat*, …)

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(PyObjectexception*, const charformat, va_listvargs*)

Return value: Always NULL.

Same as PyErr_Format(), but taking a va_list argument rather than a variable number of arguments.

3.5 新版功能.

void PyErr_SetNone(PyObject*type)

This is a shorthand for PyErr_SetObject(type, Py_None).

int PyErr_BadArgument()

This is a shorthand for PyErr_SetString(PyExc_TypeError, message), where message indicates that a built-in operation was invoked with an illegal argument. It is mostly for internal use.

PyObject* PyErr_NoMemory()

Return value: Always NULL.

This is a shorthand for PyErr_SetNone(PyExc_MemoryError); it returns NULL so an object allocation function can write return PyErr_NoMemory(); when it runs out of memory.

PyObject* PyErr_SetFromErrno(PyObject*type)

Return value: Always NULL.

This is a convenience function to raise an exception when a C library function has returned an error and set the C variable errno. It constructs a tuple object whose first item is the integer errno value and whose second item is the corresponding error message (gotten from strerror()), and then calls PyErr_SetObject(type, object). On Unix, when the errno value is EINTR, indicating an interrupted system call, this calls PyErr_CheckSignals(), and if that set the error indicator, leaves it set to that. The function always returns NULL, so a wrapper function around a system call can write return PyErr_SetFromErrno(type); when the system call returns an error.

PyObject* PyErr_SetFromErrnoWithFilenameObject(PyObject

Return value: Always NULL.

Similar to PyErr_SetFromErrno(), with the additional behavior that if filenameObject is not NULL, it is passed to the constructor of type as a third parameter. In the case of OSError exception, this is used to define the filename attribute of the exception instance.

PyObject* PyErr_SetFromErrnoWithFilenameObjects(PyObject, PyObject **filenameObject2)

Return value: Always NULL.

Similar to PyErr_SetFromErrnoWithFilenameObject(), but takes a second filename object, for raising errors when a function that takes two filenames fails.

3.4 新版功能.

PyObject* PyErr_SetFromErrnoWithFilename(PyObjecttype*, const charfilename*)

Return value: Always NULL.

Similar to PyErr_SetFromErrnoWithFilenameObject(), but the filename is given as a C string. filename is decoded from the filesystem encoding (os.fsdecode()).

PyObject* PyErr_SetFromWindowsErr(int ierr)

Return value: Always NULL.

This is a convenience function to raise WindowsError. If called with ierr of 0, the error code returned by a call to GetLastError() is used instead. It calls the Win32 function FormatMessage() to retrieve the Windows description of error code given by ierr or GetLastError(), then it constructs a tuple object whose first item is the ierr value and whose second item is the corresponding error message (gotten from FormatMessage()), and then calls PyErr_SetObject(PyExc_WindowsError, object). This function always returns NULL.

可用性: Windows。

PyObject* PyErr_SetExcFromWindowsErr(PyObject*type, int ierr)

Return value: Always NULL.

Similar to PyErr_SetFromWindowsErr(), with an additional parameter specifying the exception type to be raised.

可用性: Windows。

PyObject* PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename)

Return value: Always NULL.

Similar to PyErr_SetFromWindowsErrWithFilenameObject(), but the filename is given as a C string. filename is decoded from the filesystem encoding (os.fsdecode()).

可用性: Windows。

PyObject* PyErr_SetExcFromWindowsErrWithFilenameObject(PyObjecttype, intierr*, PyObjectfilename*)

Return value: Always NULL.

Similar to PyErr_SetFromWindowsErrWithFilenameObject(), with an additional parameter specifying the exception type to be raised.

可用性: Windows。

PyObject* PyErr_SetExcFromWindowsErrWithFilenameObjects(PyObjecttype, intierr*, PyObjectfilename, PyObject **filename2)

Return value: Always NULL.

Similar to PyErr_SetExcFromWindowsErrWithFilenameObject(), but accepts a second filename object.

可用性: Windows。

3.4 新版功能.

PyObject* PyErr_SetExcFromWindowsErrWithFilename(PyObjecttype, intierr*, const charfilename*)

Return value: Always NULL.

Similar to PyErr_SetFromWindowsErrWithFilename(), with an additional parameter specifying the exception type to be raised.

可用性: Windows。

PyObject* PyErr_SetImportError(PyObject

Return value: Always NULL.

This is a convenience function to raise ImportError. msg will be set as the exception’s message string. name and path, both of which can be NULL, will be set as the ImportError‘s respective name and path attributes.

3.3 新版功能.

void PyErr_SyntaxLocationObject(PyObject*filename, int lineno, int col_offset)

Set file, line, and offset information for the current exception. If the current exception is not a SyntaxError, then it sets additional attributes, which make the exception printing subsystem think the exception is a SyntaxError.

3.4 新版功能.

void PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)

Like PyErr_SyntaxLocationObject(), but filename is a byte string decoded from the filesystem encoding (os.fsdecode()).

3.2 新版功能.

void PyErr_SyntaxLocation(const char *filename, int lineno)

Like PyErr_SyntaxLocationEx(), but the col_offset parameter is omitted.

void PyErr_BadInternalCall()

This is a shorthand for PyErr_SetString(PyExc_SystemError, message), where message indicates that an internal operation (e.g. a Python/C API function) was invoked with an illegal argument. It is mostly for internal use.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值