Namespaces
Variants

Error handling

From cppreference.net
< 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 类型的类型定义,用于自文档化返回 errno 值的函数
(类型定义)
定义于头文件 <stddef.h>
定义于头文件 <stdio.h>
定义于头文件 <stdlib.h>
定义于头文件 <string.h>
定义于头文件 <time.h>
定义于头文件 <wchar.h>
rsize_t
(C11)
size_t 同类型的类型定义,用于自文档化在运行时检查参数范围的函数
(类型定义)
定义于头文件 <stdint.h>
RSIZE_MAX
(C11)
边界检查函数可接受的最大尺寸,可展开为常量或可能在运行时改变的变量(例如随当前分配内存大小变化)
(宏变量)
定义于头文件 <stdlib.h>
设置边界检查函数的错误回调
(函数)
边界检查函数的中止回调
(函数)
边界检查函数的忽略回调
(函数)

注意:边界检查函数的实现可作为开源库 Safe C Slibc 使用,并作为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++ 文档 关于 错误处理