std::format_error
来自 cppreference.cn
定义于头文件 <format> |
||
class format_error; |
(since C++20) | |
定义了异常对象的类型,该对象在格式化库中报告错误时抛出。
继承关系图
目录 |
[编辑] 成员函数
(构造函数) |
使用给定的消息构造一个新的 format_error 对象(公共成员函数) |
operator= |
替换 format_error 对象(公共成员函数) |
std::format_error::format_error
format_error( const std::string& what_arg ); |
(1) | |
format_error( const char* what_arg ); |
(2) | |
format_error( const format_error& other ) noexcept; |
(3) | |
3) 复制构造函数。如果 *this 和 other 均具有动态类型
std::format_error
,则 std::strcmp(what(), other.what()) == 0。复制构造函数不能抛出异常。参数
what_arg | - | 解释性字符串 |
other | - | 要复制的另一个异常对象 |
异常
1,2) 可能抛出 std::bad_alloc。
注解
由于不允许复制 std::format_error
抛出异常,因此该消息通常在内部存储为单独分配的引用计数字符串。这也是为什么没有接受 std::string&&
的构造函数的原因:无论如何它都必须复制内容。
派生的标准异常类必须具有公共可访问的复制构造函数。只要原始对象和复制对象通过 what()
获取的解释性字符串相同,它就可以隐式定义。
std::format_error::operator=
format_error& operator=( const format_error& other ) noexcept; |
||
将内容赋值为 other 的内容。如果 *this 和 other 均具有动态类型 std::format_error
,则赋值后 std::strcmp(what(), other.what()) == 0。复制赋值运算符不能抛出异常。
参数
other | - | 要赋值的另一个异常对象 |
返回值
*this
注解
派生的标准异常类必须具有公共可访问的复制赋值运算符。只要原始对象和复制对象通过 what()
获取的解释性字符串相同,它就可以隐式定义。
继承自 std::exception
成员函数
[virtual] |
销毁异常对象 ( std::exception 的虚公共成员函数) |
[virtual] |
返回解释性字符串 ( std::exception 的虚公共成员函数) |
[编辑] 示例
运行此代码
#include <format> #include <print> #include <string_view> #include <utility> int main() { try { auto x13{37}; auto args{std::make_format_args(x13)}; std::ignore = std::vformat("{:()}", args); // throws } catch(const std::format_error& ex) { std::println("{}", ex.what()); } }
可能的输出
format error: failed to parse format-spec