std::format_error
來自 cppreference.com
| 定義於標頭檔案 <format> |
||
| class format_error; |
(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 | - | 解釋性字串 |
| 其他 | - | 要複製的另一個異常物件 |
異常
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。複製賦值運算子不能丟擲異常。
引數
| 其他 | - | 用於賦值的另一個異常物件 |
返回值
*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