std::ios_base::failure - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::ios_base::failure

From cppreference.com
< cpp‎ | io‎ | ios base

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.

Inheritance diagram | (until C++11)
Inheritance diagram | (since C++11)

### [edit] Member functions

(constructor) | constructs the exception object (public member function) [edit]

# 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

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

## Inherited from std::runtime_error

## Inherited from std::exception

### Member functions

(destructor) [virtual] | destructs the exception object (virtual public member function of `std::exception`) [edit]
what [virtual] | returns an explanatory string (virtual public member function of `std::exception`) [edit]

### [edit] Example

Run this code

    #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

### [edit] See also

io_errc (C++11) | the IO stream error codes (enum) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/io/ios_base/failure&oldid=62100"