std::ios_base::failure

Header: <ios>

The class std::ios_base::failure defines an exception object that is thrown on failure by the functions in the Input/Output library.

# Declarations

class failure;

# Parameters

# Return value

*this

# Notes

Because copying std::ios_base::failure 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.

# Example

#include <fstream>
#include <iostream>
 
int main()
{
    std::ifstream f("doesn't exist");
 
    try
    {
        f.exceptions(f.failbit);
    }
    catch (const std::ios_base::failure& e)
    {
        std::cout << "Caught an ios_base::failure.\n"
                  << "Explanatory string: " << e.what() << '\n'
                  << "Error code: " << e.code() << '\n';
    }
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 48C++98the constructor overload (1) initialized the base class std::exceptionwith msg, but the base class does not have a matching constructorcorrespondingdescription removed
LWG 331C++98std::ios_base::failure declared a destructor without throw()removed the destructor declaration

# See also