名稱空間
變體
操作

std::overflow_error

來自 cppreference.com
< cpp‎ | 錯誤
定義於標頭檔案 <stdexcept>
class overflow_error;

定義一種作為異常丟擲的物件型別。它可用於報告算術溢位錯誤(即,計算結果對於目標型別而言過大的情況)。

唯一丟擲此異常的標準庫元件是 std::bitset::to_ulong

(C++11 前)

唯一丟擲此異常的標準庫元件是 std::bitset::to_ulongstd::bitset::to_ullong

(C++11 起)

標準庫元件的數學函式不丟擲此異常(數學函式按照 math_errhandling 中指定的方式報告溢位錯誤)。然而,第三方庫會使用此異常。例如,如果啟用了 boost::math::policies::throw_on_error(預設設定),boost.math 會丟擲 std::overflow_error

cpp/error/exceptioncpp/error/runtime errorstd-overflow error-inheritance.svg

繼承圖

目錄

[編輯] 成員函式

(建構函式)
用給定訊息構造一個新的 overflow_error 物件
(公開成員函式)
operator=
替換 overflow_error 物件
(公開成員函式)

std::overflow_error::overflow_error

overflow_error( const std::string& what_arg );
(1)
overflow_error( const char* what_arg );
(2)
overflow_error( const overflow_error& other );
(3) (C++11 起無異常丟擲)
1) 構造異常物件,以 what_arg 作為解釋性字串。構造後,std::strcmp(what(), what_arg.c_str()) == 0
2) 構造異常物件,以 what_arg 作為解釋性字串。構造後,std::strcmp(what(), what_arg) == 0
3) 複製建構函式。如果 *thisother 都具有動態型別 std::overflow_error,則 std::strcmp(what(), other.what()) == 0。複製建構函式不能丟擲異常。

引數

what_arg - 解釋性字串
其他 - 要複製的另一個異常物件

異常

1,2) 可能丟擲 std::bad_alloc

注意

因為不允許複製 std::overflow_error 丟擲異常,所以此訊息通常作為單獨分配的引用計數字符串在內部儲存。這也是為什麼沒有接受 std::string&& 的建構函式:無論如何它都必須複製內容。

LWG issue 254 解決之前,非複製建構函式只能接受 std::string。它使動態分配成為強制性,以便構造一個 std::string 物件。

LWG issue 471 解決之後,派生的標準異常類必須具有公開可訪問的複製建構函式。只要透過 what() 獲取的解釋性字串對於原始物件和複製物件相同,就可以隱式定義它。

std::overflow_error::operator=

overflow_error& operator=( const overflow_error& other );
(C++11 起無異常丟擲)

將內容賦值為 other 的內容。如果 *thisother 都具有動態型別 std::overflow_error,則賦值後 std::strcmp(what(), other.what()) == 0。複製賦值運算子不能丟擲異常。

引數

其他 - 用於賦值的另一個異常物件

返回值

*this

注意

LWG issue 471 解決之後,派生的標準異常類必須具有公開可訪問的複製賦值運算子。只要透過 what() 獲取的解釋性字串對於原始物件和複製物件相同,就可以隱式定義它。

繼承自 std::runtime_error


繼承自 std::exception

成員函式

銷燬異常物件
(std::exception 的虛公共成員函式) [編輯]
[virtual]
返回解釋字串
(std::exception 的虛公共成員函式) [編輯]

[編輯] 示例

#include <iostream>
#include <limits>
#include <stdexcept>
#include <utility>
 
template<typename T, int N>
    requires (N > 0) /*...*/
class Stack
{
    int top_{-1};
    T data_[N];
 
public:
    [[nodiscard]] bool empty() const { return top_ == -1; }
 
    void push(T x)
    {
        if (top_ == N - 1)
            throw std::overflow_error("Stack overflow!");
        data_[++top_] = std::move(x);
    }
 
    void pop()
    {
        if (empty())
            throw std::underflow_error("Stack underflow!");
        --top_;
    }
 
    T const& top() const
    {
        if (empty())
            throw std::overflow_error("Stack is empty!");
        return data_[top_];
    }
};
 
int main()
{
    Stack<int, 4> st;
 
    try
    {
        [[maybe_unused]] auto x = st.top();
    }
    catch (std::overflow_error const& ex)
    {
        std::cout << "1) Exception: " << ex.what() << '\n';
    }
 
    st.push(1337);
    while (!st.empty())
    	st.pop();
 
    try
    {
        st.pop();
    }
    catch (std::underflow_error const& ex)
    {
        std::cout << "2) Exception: " << ex.what() << '\n';
    }
 
    try
    {
        for (int i{}; i != 13; ++i)
            st.push(i);
    }
    catch (std::overflow_error const& ex)
    {
        std::cout << "3) Exception: " << ex.what() << '\n';
    }
}

輸出

1) Exception: Stack is empty!
2) Exception: Stack underflow!
3) Exception: Stack overflow!

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 254 C++98 缺少接受 const char* 的建構函式 已新增
LWG 471 C++98 std::overflow_error 的解釋性字串
解釋性字串是實現定義的
它們與原始 std::runtime_error 物件的
原始 std::overflow_error 物件