std::exception
來自 cppreference.com
定義於標頭檔案 <exception> |
||
class exception; |
||
提供透過 throw 表示式處理錯誤的一致介面。
標準庫生成的所有異常都繼承自 std::exception
。
|
(C++26 起) |
目錄 |
[編輯] 成員函式
構造異常物件 (公開成員函式) | |
[虛] |
銷燬異常物件 (虛公開成員函式) |
複製異常物件 (公開成員函式) | |
[虛] |
返回解釋字串 (虛公開成員函式) |
[編輯] 標準異常要求
每個派生自 std::exception
的標準庫類 T
都擁有下列公開可訪問的成員函式,它們中的每一個均不以異常退出(C++11 前)都擁有不丟擲異常的異常規範(C++11 起)
複製建構函式和複製賦值運算子滿足以下後置條件
- 如果兩個物件 lhs 和 rhs 都具有動態型別
T
且 lhs 是 rhs 的一個副本,那麼 std::strcmp(lhs.what(), rhs.what()) 等於 0。
每個這樣的 T
的 what()
成員函式均滿足為 std::exception::what() 所指定的約束。
[編輯] 標準異常
- range_error
- overflow_error
- underflow_error
- regex_error (C++11 起)
- system_error (C++11 起)
- ios_base::failure (C++11 起)
- filesystem::filesystem_error (C++17 起)
- tx_exception (TM TS)
- nonexistent_local_time (C++20 起)
- ambiguous_local_time (C++20 起)
- format_error (C++20 起)
- bad_any_cast (C++17 起)
- bad_optional_access (C++17 起)
- bad_expected_access (C++23 起)
- bad_weak_ptr (C++11 起)
- bad_function_call (C++11 起)
- bad_alloc
- bad_array_new_length (C++11 起)
- bad_exception
- ios_base::failure (C++11 前)
- bad_variant_access (C++17 起)
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_constexpr_exceptions |
202411L |
(C++26) | 異常型別的 constexpr |
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 471 | C++98 | 對於從 std::exception 派生的標準庫類沒有要求 |
已新增 |