std::ios_base::failure(3)
π NAME π
std::ios_base::failure β yes bestie, the exception class itself. This page is std::ios_base::failure(3) from the C++ Standard Library, and we are absolutely gonna make it make sense.
β¨ Synopsis β¨
Defined in header <ios>:
class failure;
The class std::ios_base::failure defines an exception object that is thrown on failure by the functions in the Input/Output library.
std::ios_base::failure may be defined either as a member class of std::ios_base or as a synonym (typedef) for another class with equivalent functionality. (since C++17)
The page also drops these inheritance-diagram image filenames, so here they are exactly:
std-ios base-failure-2003-inheritance.svgβ Inheritance diagram (until C++11)std-ios base-failure-inheritance.svgβ Inheritance diagram (since C++11)
π Member functions π
- constructor β constructs a new failure object with the given message (public member function)
- operator= β replaces the failure object (public member function)
- what β returns the explanatory string (public member function)
πΈ std::ios_base::failure::failure πΈ
The pageβs signature block is a little chaotic, so here it is exactly as printed β do not let the line breaks gaslight you:
explicit failure( const std::string& message ); (until C++11)
explicit failure( const std::string& message,
const std::error_code& ec = (since C++11)
std::io_errc::stream );
explicit failure( const char* message, (1)
const std::error_code& ec = (2) (since C++11)
std::io_errc::stream );
failure( const failure& other ); (3)
(until C++11)
failure( const failure& other ) noexcept;
(since C++11)
- 1, 2) Constructs the exception object using
messageas the explanation string, which can later be retrieved usingwhat().ecis used to identify the specific reason for the failure. (since C++11) - 3) Copy constructor. Initialize the contents with those of
other. If*thisandotherboth have dynamic typestd::ios_base::failurethenstd::strcmp(what(), other.what()) == 0. (since C++11)
Parameters
- message β explanatory string
- ec β error code to identify the specific reason for the failure
- other β another failure to copy
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.
π std::ios_base::failure::operator= π
failure& operator=( const failure& other ); (until C++11)
failure& operator=( const failure& other ) noexcept; (since C++11)
Assigns the contents with those of other. If *this and other both have dynamic type std::ios_base::failure then std::strcmp(what(), other.what()) == 0 after assignment. (since C++11)
Parameters
- other β another exception object to assign with
Return value
*this
π¦ std::ios_base::failure::what π¦
virtual const char* what() const throw(); (until C++11)
virtual const char* what() const noexcept; (since C++11)
Returns the explanatory string.
Parameters
(none)
Return value
Pointer to a null-terminated string with explanatory information. The string is suitable for conversion and display as a std::wstring. The pointer is guaranteed to be valid at least until the exception object from which it is obtained is destroyed, or until a non-const member function (e.g. copy assignment operator) on the exception object is called.
Notes
Implementations are allowed but not required to override what().
π Inherited from std::system_error π
Member functions
codeβ returns error code (public member function ofstd::system_error)whatβ returns an explanatory string[virtual](virtual public member function ofstd::system_error)
π Inherited from std::runtime_error π
That's the whole vibe here β the page just says it inherits from std::runtime_error, and no extra member-function list is printed under it.
π§ Inherited from std::exception π§
Member functions
- destructor β destroys the exception object
[virtual](virtual public member function ofstd::exception) whatβ returns an explanatory string[virtual](virtual public member function ofstd::exception)
π Notes π
Before the resolution of LWG issue 331, std::ios_base::failure declared a destructor without throw(), where std::exception::~exception() was declared with throw()^[1]. This means the std::ios_base::failure::~failure() had a weaker exception specification. The resolution is to remove that declaration so that the non-throwing exception specification is kept.
LWG issue 363 targets the same defect and its resolution is to add throw() to the declaration of std::ios_base::failure::~failure(). That resolution was not applied due to the conflict between the two resolutions.
- β The non-throwing exception specification is now applied globally across the standard library, so the destructors of standard library classes are not declared with
throw()ornoexcept.
π₯ Example π₯
// Run this code
#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';
}
}
β Possible output β
Caught an ios_base::failure.
Explanatory string: ios_base::clear: unspecified iostream_category error
Error code: iostream:1
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR: LWG 48 β applied to C++98 β behavior as published: the constructor overload (1) initialized the base class
std::exceptionwithmsg, but the base class does not have a matching constructor β correct behavior: corresponding description removed. - DR: LWG 331 β applied to C++98 β behavior as published:
std::ios_base::failuredeclared a destructor withoutthrow()β correct behavior: removed the destructor declaration.
π See also π
io_errcβ the IO stream error codes (C++11) (enum)
Source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::ios_base::failure(3). That's the whole manual page, bestie β go forth and catch those exceptions π
β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms