std::ios_base::failure

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).

cpp/error/exceptionstd-ios base-failure-2003-inheritance.svg

Inheritance diagram.

(until C++11)
cpp/error/exception cpp/error/runtime error cpp/error/system errorstd-ios base-failure-inheritance.svg

Inheritance diagram.

(since C++11)

Member functions

(constructor)
constructs the exception object
(public member function)

std::ios_base::failure::failure

explicit failure( const std::string& message );
(until C++11)
explicit failure( const std::string& message, 
                  const std::error_code& ec = std::io_errc::stream );
(since C++11)
explicit failure( const char* message,
                  const std::error_code& ec = std::io_errc::stream );
(since C++11)

Constructs the exception object using message as explanation string which can later be retrieved using what().

Parameters

message - explanatory string

Inherited from std::system_error

Member functions

returns error code
(public member function of std::system_error)
[virtual]
returns explanatory string
(virtual public member function of std::system_error)

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 <fstream>
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';
    }
}

Output:

Caught an ios_base::failure.
Explanatory string: ios_base::clear: unspecified iostream_category error
Error code: iostream:1

See also

(C++11)
the IO stream error codes
(enum)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/io/ios_base/failure