名前空間
変種
操作

std::overflow_error

From cppreference.com
< cpp‎ | error
 
 
 
ヘッダー <stdexcept> で定義
class overflow_error;

例外としてスローされるオブジェクトの型を定義します。算術オーバーフローエラー(つまり、計算結果が宛先型に対して大きすぎる状況)を報告するために使用できます。

この例外をスローする唯一の標準ライブラリコンポーネントは、std::bitset::to_ulong です。

(C++11まで)

この例外をスローする唯一の標準ライブラリコンポーネントは、std::bitset::to_ulongstd::bitset::to_ullong です。

(C++11以降)

標準ライブラリコンポーネントの数学関数はこの例外をスローしません(数学関数は math_errhandling で指定されたとおりにオーバーフローエラーを報告します)。しかし、サードパーティライブラリはこの例外を使用します。例えば、boost.math は、boost::math::policies::throw_on_error が有効になっている場合(デフォルト設定)、std::overflow_error をスローします。

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

継承図

目次

[編集] メンバ関数

(コンストラクタ)
指定されたメッセージを持つ新しい overflow_error オブジェクトを構築します。
(public member function)
operator=
overflow_error オブジェクトを置き換えます。
(public member function)

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 以降 noexcept)
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 以降 noexcept)

other の内容で内容を代入します。*thisother が両方とも動的型 std::overflow_error を持つ場合、代入後に std::strcmp(what(), other.what()) == 0 です。コピー代入演算子から例外をスローすることはできません。

パラメータ

その他 - 割り当てる別の例外オブジェクト

戻り値

*this

注釈

LWG issue 471 の解決後、派生標準例外クラスは公開アクセス可能なコピー代入演算子を持つ必要があります。what() によって取得される説明文字列が元のオブジェクトとコピーされたオブジェクトで同じである限り、暗黙的に定義することができます。

std::runtime_error から継承


std::exception から継承

メンバ関数

例外オブジェクトを破棄する
(std::exception の仮想 public メンバー関数) [編集]
[virtual]
説明文字列を返す
(std::exception の仮想 public メンバー関数) [編集]

[編集]

#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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 254 C++98 const char* を受け入れるコンストラクタが欠落していた 追加された
LWG 471 C++98 std::overflow_error の説明文字列。
コピーは実装定義であった
それらは元の std::domain_error オブジェクトと
元の std::overflow_error オブジェクト。
English 日本語 中文(简体) 中文(繁體)