Namespaces
Variants

std::ios_base:: failure

From cppreference.net
ヘッダーで定義 <ios>
class failure ;

std::ios_base::failure クラスは、入出力ライブラリの関数が失敗した際にスローされる例外オブジェクトを定義します。

std::ios_base::failure は、 std::ios_base のメンバークラスとして定義されるか、または同等の機能を持つ別のクラスのシノニム(typedef)として定義される可能性があります。

(C++17以降)
cpp/error/exception std-ios base-failure-2003-inheritance.svg

継承図

(C++11以前)
cpp/error/exception cpp/error/runtime error cpp/error/system error std-ios base-failure-inheritance.svg

継承図

(C++11以降)

目次

メンバー関数

(constructor)
指定されたメッセージで新しい failure オブジェクトを構築する
(public member function)
operator=
failure オブジェクトを置き換える
(public member function)
what
説明文字列を返す
(public member function)

std::ios_base::failure:: failure

(1)
explicit failure ( const std:: string & message ) ;
(C++11以前)
explicit failure ( const std:: string & message,
const std:: error_code & ec = std:: io_errc :: stream ) ;
(C++11以降)
explicit failure ( const char * message,
const std:: error_code & ec = std:: io_errc :: stream ) ;
(2) (C++11以降)
(3)
failure ( const failure & other ) ;
(C++11以前)
failure ( const failure & other ) noexcept ;
(C++11以降)
1,2) 例外オブジェクトを構築し、 message を説明文字列として使用します。この文字列は後で what() を使用して取得できます。 ec は失敗の特定の理由を識別するために使用されます。 (C++11以降)
3) コピーコンストラクタ。 other の内容で初期化します。 * this other の両方が動的型 std::ios_base::failure を持つ場合、 std:: strcmp ( what ( ) , other. what ( ) ) == 0 となります。 (C++11以降)

パラメータ

message - 説明文字列
ec - 失敗の特定の理由を識別するエラーコード
other - コピーする別の failure

注記

std::ios_base::failure のコピーは例外をスローすることが許可されていないため、このメッセージは通常、別途割り当てられた参照カウント方式の文字列として内部的に保存されます。これが、 std:: string && を受け取るコンストラクタが存在しない理由でもあります。いずれにせよ内容をコピーする必要があるためです。

std::ios_base::failure:: operator=

failure & operator = ( const failure & other ) ;
(C++11まで)
failure & operator = ( const failure & other ) noexcept ;
(C++11以降)

other の内容を代入します。 * this other の両方が動的型 std::ios_base::failure を持つ場合、代入後は std:: strcmp ( what ( ) , other. what ( ) ) == 0 となります。 (C++11以降)

パラメータ

other - 代入する別の例外オブジェクト

戻り値

* this

std::ios_base::failure:: what

virtual const char * what ( ) const throw ( ) ;
(C++11以前)
virtual const char * what ( ) const noexcept ;
(C++11以降)

説明文字列を返します。

戻り値

説明情報を含む実装定義のナル終端文字列へのポインタ。この文字列は std::wstring への変換と表示に適しています。ポインタは、少なくとも取得元の例外オブジェクトが破棄されるまで、または例外オブジェクトの非constメンバ関数(コピー代入演算子など)が呼び出されるまで有効であることが保証されます。

注記

実装は what() をオーバーライドすることが許可されていますが、必須ではありません。

std::system_errorから継承

メンバ関数

エラーコードを返す
( std::system_error のpublicメンバ関数)
[virtual]
説明文字列を返す
( std::system_error のvirtual publicメンバ関数)

std::exception から継承 std:: exception

メンバ関数

[virtual]
例外オブジェクトを破棄
( std::exception の仮想publicメンバ関数)
[virtual]
説明文字列を返す
( std::exception の仮想publicメンバ関数)

注記

LWG issue 331 が解決される以前は、 std::ios_base::failure throw ( ) なしでデストラクタを宣言していましたが、 std::exception::~exception() throw ( ) 付きで宣言されていました [1] 。これは std::ios_base::failure::~failure() の例外仕様が弱いことを意味していました。解決策として、その宣言を削除し、非スロー例外仕様を維持することとされました。

LWG issue 363 は同じ欠陥を対象としており、その解決策は throw ( ) std::ios_base::failure::~failure() の宣言に追加することです。この解決策は、両方の解決策間の競合により適用されませんでした。

  1. 非スロー例外仕様は現在 標準ライブラリ全体にわたってグローバルに適用 されているため、標準ライブラリクラスのデストラクタは throw ( ) または noexcept で宣言されていません。

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

出力例:

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

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 48 C++98 コンストラクタのオーバーロード (1) が基底クラス std::exception
msg で初期化していたが、基底クラスには一致するコンストラクタが存在しない
対応する
記述を削除
LWG 331 C++98 std::ios_base::failure throw ( ) なしでデストラクタを宣言していた デストラクタの宣言を削除

関連項目

(C++11)
IOストリームエラーコード
(列挙型)