命名空间
变体
操作

错误处理

来自 cppreference.cn
< c

目录

[编辑] 错误编号

定义于头文件 <errno.h>
宏,展开为 POSIX 兼容的线程局部错误编号变量
(宏变量) [编辑]
标准 POSIX 兼容错误条件的宏
(宏常量) [编辑]

[编辑] 断言

定义于头文件 <assert.h>
如果用户指定的条件不为 true,则中止程序。 可以为发布版本禁用
(函数宏) [编辑]
(C11)(C23 中移除)
如果常量表达式的值为 false,则发出编译时诊断
(关键字宏)

边界检查

标准库提供了一些现有函数的边界检查版本 (gets_s, fopen_s, printf_s, strcpy_s, wcscpy_s, mbstowcs_s, qsort_s, getenv_s, 等等)。 此功能是可选的,并且仅当定义了 __STDC_LIB_EXT1__ 时才可用。 以下宏和函数支持此功能。

 
定义于头文件 <errno.h>
定义于头文件 <stdio.h>
errno_t
(C11)
类型 int 的 typedef,用于自文档化返回 errno 值的函数
(typedef)
 
定义于头文件 <stddef.h>
定义于头文件 <stdio.h>
定义于头文件 <stdlib.h>
定义于头文件 <string.h>
定义于头文件 <time.h>
定义于头文件 <wchar.h>
rsize_t
(C11)
size_t 类型相同的 typedef,用于自文档化在运行时范围检查其参数的函数
(typedef)
 
定义于头文件 <stdint.h>
RSIZE_MAX
(C11)
边界检查函数可接受的最大大小,展开为常量或变量,可能会在运行时更改 (例如,随着当前分配的内存大小更改)
(宏变量)
 
定义于头文件 <stdlib.h>
为边界检查函数设置错误回调
(函数) [编辑]
边界检查函数的中止回调
(函数) [编辑]
边界检查函数的忽略回调
(函数) [编辑]

注意:边界检查函数的实现可以作为开源库 Safe CSlibc 以及 Watcom C 的一部分提供。 Visual Studio 中也有一组不兼容的边界检查函数。

(自 C11 起)

[编辑] 注释

自 C23 起,static_assert 本身是一个关键字,也可能是一个预定义的宏,因此 <assert.h> 不再提供它。

[编辑] 参考文献

扩展内容
  • C23 标准 (ISO/IEC 9899:2024)
  • 7.2 诊断 <assert.h> (页码: 待定)
  • 7.5 错误 <errno.h> (页码: 待定)
  • 7.19 常用定义 <stddef.h> (页码: 待定)
  • 7.20 整数类型 <stdint.h> (页码: 待定)
  • 7.21 输入/输出 <stdio.h> (页码: 待定)
  • 7.22 通用实用工具 <stdlib.h> (页码: 待定)
  • K.3.1.3 errno 的使用 (页码: 待定)
  • K.3.2/2 errno_t (页码: 待定)
  • K.3.3/2 rsize_t (页码: 待定)
  • K.3.4/2 RSIZE_MAX (页码: 待定)
  • 7.31.3 错误 <errno.h> (页码: 待定)
  • 7.31.10 整数类型 <stdint.h> (页码: 待定)
  • 7.31.11 输入/输出 <stdio.h> (页码: 待定)
  • 7.31.12 通用实用工具 <stdlib.h> (页码: 待定)
  • C17 标准 (ISO/IEC 9899:2018)
  • 7.2 诊断 <assert.h> (页码: 待定)
  • 7.5 错误 <errno.h> (页码: 待定)
  • 7.19 常用定义 <stddef.h> (页码: 待定)
  • 7.20 整数类型 <stdint.h> (页码: 待定)
  • 7.21 输入/输出 <stdio.h> (页码: 待定)
  • 7.22 通用实用工具 <stdlib.h> (页码: 待定)
  • K.3.1.3 errno 的使用 (页码: 待定)
  • K.3.2/2 errno_t (页码: 待定)
  • K.3.3/2 rsize_t (页码: 待定)
  • K.3.4/2 RSIZE_MAX (页码: 待定)
  • 7.31.3 错误 <errno.h> (页码: 待定)
  • 7.31.10 整数类型 <stdint.h> (页码: 待定)
  • 7.31.11 输入/输出 <stdio.h> (页码: 待定)
  • 7.31.12 通用实用工具 <stdlib.h> (页码: 待定)
  • C11 标准 (ISO/IEC 9899:2011)
  • 7.2 诊断 <assert.h> (页码: 186-187)
  • 7.5 错误 <errno.h> (页码: 205)
  • 7.19 常用定义 <stddef.h> (页码: 288)
  • 7.20 整数类型 <stdint.h> (页码: 289-295)
  • 7.21 输入/输出 <stdio.h> (页码: 296-339)
  • 7.22 通用实用工具 <stdlib.h> (页码: 340-360)
  • K.3.1.3 errno 的使用 (页码: 584)
  • K.3.2/2 errno_t (页码: 585)
  • K.3.3/2 rsize_t (页码: 585)
  • K.3.4/2 RSIZE_MAX (页码: 585)
  • 7.31.3 错误 <errno.h> (页码: 455)
  • 7.31.10 整数类型 <stdint.h> (页码: 456)
  • 7.31.11 输入/输出 <stdio.h> (页码: 456)
  • 7.31.12 通用实用工具 <stdlib.h> (页码: 456)
  • C99 标准 (ISO/IEC 9899:1999)
  • 7.2 诊断 <assert.h> (页码: 169)
  • 7.5 错误 <errno.h> (页码: 186)
  • 7.26.3 错误 <errno.h> (页码: 401)
  • 7.26.8 整数类型 <stdint.h> (页码: 401)
  • 7.26.9 输入/输出 <stdio.h> (页码: 402)
  • 7.26.10 通用实用工具 <stdlib.h> (页码: 402)
  • C89/C90 标准 (ISO/IEC 9899:1990)
  • 4.2 诊断 <assert.h>
  • 4.1.3 错误 <errno.h>
  • 4.13.1 错误 <errno.h>
  • 4.13.6 输入/输出 <stdio.h>
  • 4.13.7 通用实用工具 <stdlib.h>

[编辑] 参见

定义了通用数学函数使用的错误处理机制
(宏常量) [编辑]
C++ 文档 关于 错误处理