来自 exception::what() 的异常描述是否针对标准异常进行了标准化
Are exception descriptions from exception::what() standarized for standard exceptions?
对于像 std::bad_alloc
这样的标准化异常,exception::what()
返回的字符串是否应该在编译器中相同?
例如,我使用过的几个编译器都返回bad allocation
bad_alloc::what()
。 是否有某种原因没有返回更具描述性的字符串(如 Memory allocation failure
)?
exception::what()
返回的字符串是否应该在编译器之间相同?
不,这不是标准化的。
以您的bad_alloc
为例:
[C++11: 18.6.2.1/5]:
virtual const char* what() const noexcept;
返回:实现定义的 NTBS。
(NTBS 表示"以 null 结尾的字节字符串";它是你关心的"实现定义的"。
充其量,此模式在所有标准异常定义中找到。
是否有某种原因没有返回更具描述性的字符串(如内存分配失败)?
可能是因为它更长,实际上没有更多的描述性。像"内存分配失败"这样的冗长字符串是非常不必要的;在调用代码中,始终可以扩展提供信息的短字符串,使其更加人性化。
没有标准,这完全取决于C++运行时的实现。
相关文章:
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- C++ 捕获异常后进行清理的标准方法是什么?
- 为什么标准不允许通过引用捕获不完整的异常类型?
- 关于异常,覆盖标准异常方法
- 标准库函数 abs() 在不同C++编译器上的异常行为
- 标准::unique_ptr和异常安全
- 来自 exception::what() 的异常描述是否针对标准异常进行了标准化
- 我在哪里可以找到标准容器和算法的所有异常保证
- 异常规范和标准输入迭代器
- 在混合C++中捕获标准异常和系统异常
- 捕获标准::异常和ERROR_STATUS
- 标准::螺纹(可拆卸)和异常安全
- C++错误 C2040:"e":"记录器"的间接级别与"常量标准::异常 &"不同
- 格式化的输出运算符标准iostream对象可以抛出什么样的异常
- MSVC std::异常不符合标准?
- 如何捕获C++标准17.6.4.10[res.on.exception.handing]中描述的异常
- 我们应该使用标准库中的异常类型吗?
- c++标准是否允许添加两个整数(基本类型int)来抛出c++异常?
- 如何对抛出哪个标准异常做出明智的决定
- 是否有事实上的标准例外或最佳实践方法来发出异常"invalid input"信号?