W3cubDocs

/C++

std::overflow_error

Defined in header <stdexcept>
class overflow_error;

Defines a type of object to be thrown as exception. It can be used to report arithmetic overflow errors (that is, situations where a result of a computation is too large for the destination type).

The only standard library component that throws this exception is std::bitset::to_ulong.

(until C++11)

The only standard library components that throw this exception are std::bitset::to_ulong and std::bitset::to_ullong.

(since C++11)

The mathematical functions of the standard library components do not throw this exception (mathematical functions report overflow errors as specified in math_errhandling). Third-party libraries, however, use this. For example, boost.math throws std::overflow_error if boost::math::policies::throw_on_error is enabled (the default setting).

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

Inheritance diagram.

Member functions

(constructor)
constructs a new overflow_error object with the given message
(public member function)
operator=
replaces the overflow_error object
(public member function)

std::overflow_error::overflow_error

overflow_error( const std::string& what_arg );
(1)
overflow_error( const char* what_arg );
(2)
(3)
overflow_error( const overflow_error& other );
(until C++11)
overflow_error( const overflow_error& other ) noexcept;
(since C++11)
1) Constructs the exception object with what_arg as explanatory string. After construction, std::strcmp(what(), what_arg.c_str()) == 0.
2) Constructs the exception object with what_arg as explanatory string. After construction, std::strcmp(what(), what_arg) == 0.
3) Copy constructor. If *this and other both have dynamic type std::overflow_error then std::strcmp(what(), other.what()) == 0. No exception can be thrown from the copy constructor. (until C++11)

Parameters

what_arg - explanatory string
other - another exception object to copy

Exceptions

1-2) May throw std::bad_alloc

Notes

Because copying std::overflow_error is not permitted to throw exceptions, this message is typically stored internally as a separately-allocated reference-counted string. This is also why there is no constructor taking std::string&&: it would have to copy the content anyway.

Before the resolution of LWG issue 254, the non-copy constructor can only accept std::string. It makes dynamic allocation mandatory in order to construct a std::string object.

After the resolution of LWG issue 471, a derived standard exception class must have a publicly accessible copy constructor. It can be implicitly defined as long as the explanatory strings obtained by what() are the same for the original object and the copied object.

std::overflow_error::operator=

overflow_error& operator=( const overflow_error& other );
(until C++11)
overflow_error& operator=( const overflow_error& other ) noexcept;
(since C++11)

Assigns the contents with those of other. If *this and other both have dynamic type std::overflow_error then std::strcmp(what(), other.what()) == 0 after assignment. No exception can be thrown from the copy assignment operator. (until C++11).

Parameters

other - another exception object to assign with

Return value

*this.

Notes

After the resolution of LWG issue 471, a derived standard exception class must have a publicly accessible copy assignment operator. It can be implicitly defined as long as the explanatory strings obtained by what() are the same for the original object and the copied object.

Inherited from std::runtime_error

Inherited from std::exception

Member functions

[virtual]
destroys the exception object
(virtual public member function of std::exception)
[virtual]
returns an explanatory string
(virtual public member function of std::exception)

Example

#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::overflow_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 (not st.empty())
            st.pop();
 
    try
    {
        st.pop();
    }
    catch (std::overflow_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';
    }
}

Output:

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

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 254 C++98 the constructor accepting const char* was missing added
LWG 471 C++98 the explanatory strings of std::overflow_error's
copies were implementation-defined
they are the same as that of the
original std::overflow_error object

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/error/overflow_error