std::logic_error
來自 cppreference.com
定義於標頭檔案 <stdexcept> |
||
class logic_error; |
||
定義一種作為異常丟擲的物件型別。它報告由於程式內部邏輯錯誤(例如違反邏輯前置條件或類不變式)而導致的錯誤,並且可能是可預防的。
沒有標準庫元件直接丟擲此異常,但異常型別 std::invalid_argument、std::domain_error、std::length_error、std::out_of_range、std::future_error 和 std::experimental::bad_optional_access 派生自 std::logic_error
。
繼承圖
目錄 |
[編輯] 成員函式
(建構函式) |
使用給定訊息構造一個新的 logic_error 物件(公開成員函式) |
operator= |
替換 logic_error 物件(公開成員函式) |
std::logic_error::logic_error
logic_error( const std::string& what_arg ); |
(1) | |
logic_error( const char* what_arg ); |
(2) | |
logic_error( const logic_error& other ); |
(3) | (C++11 起無異常丟擲) |
3) 複製建構函式。如果 *this 和 other 都具有動態型別
std::logic_error
,則 std::strcmp(what(), other.what()) == 0。複製建構函式不能丟擲異常。引數
what_arg | - | 解釋性字串 |
其他 | - | 要複製的另一個異常物件 |
異常
1,2) 可能丟擲 std::bad_alloc。
注意
由於複製 std::logic_error
不允許丟擲異常,因此此訊息通常在內部儲存為單獨分配的引用計數字符串。這也是為什麼沒有接受 std::string&&
的建構函式:無論如何它都必須複製內容。
在 LWG issue 254 解決之前,非複製建構函式只能接受 std::string。它強制進行動態分配以構造一個 std::string 物件。
在 LWG issue 471 解決之後,派生標準異常類必須具有公開可訪問的複製建構函式。只要透過 what()
獲取的解釋字串對於原始物件和複製物件相同,它就可以隱式定義。
std::logic_error::operator=
logic_error& operator=( const logic_error& other ); |
(C++11 起無異常丟擲) | |
用 other 的內容賦值。如果 *this 和 other 都具有動態型別 std::logic_error
,則賦值後 std::strcmp(what(), other.what()) == 0。複製賦值運算子不能丟擲異常。
引數
其他 | - | 用於賦值的另一個異常物件 |
返回值
*this
注意
在 LWG issue 471 解決之後,派生標準異常類必須具有公開可訪問的複製賦值運算子。只要透過 what()
獲取的解釋字串對於原始物件和複製物件相同,它就可以隱式定義。
繼承自 std::exception
成員函式
[虛擬函式] |
銷燬異常物件 ( std::exception 的虛公共成員函式) |
[虛擬函式] |
返回解釋字串 ( std::exception 的虛公共成員函式) |
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 254 | C++98 | 缺少接受 const char* 的建構函式 | 已新增 |
LWG 471 | C++98 | std::logic_error 的解釋字串解釋性字串是實現定義的 |
它們與原始 std::runtime_error 物件的原始 std::logic_error 物件 |