std::ios_base::failure
| ヘッダ <ios>で定義 |
||
| class failure; |
||
クラス std::ios_base::failure は、入出力ライブラリの関数が失敗したときにスローされる例外オブジェクトを定義します。
|
|
(C++17以降) |
|
継承図 |
(C++11まで) |
|
継承図 |
(C++11以降) |
目次 |
[編集] メンバ関数
| (コンストラクタ) |
指定されたメッセージで新しい 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以降) | |
message を説明文字列として使用して例外オブジェクトを構築します。この文字列は後で what() を使用して取得できます。 ec は、失敗の具体的な理由を特定するために使用されます。(C++11 以降)other の内容で初期化します。 もし *this と other が両方とも動的型 std::ios_base::failure を持つ場合、代入後に std::strcmp(what(), other.what()) == 0 となります。(C++11 以降)パラメータ
| message | - | 説明文字列 |
| エラーコード | - | 失敗の具体的な理由を特定するためのエラーコード。 |
| その他 | - | コピー元の別の 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 以降)
パラメータ
| その他 | - | 割り当てる別の例外オブジェクト |
戻り値
*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 メンバー関数(例:コピー代入演算子)が呼び出されるまで、少なくとも有効であることが保証されます。
|
返された文字列は、定数評価中に通常のリテラルエンコーディングでエンコードされます。 |
(C++26以降) |
注釈
実装は what() をオーバーライドすることが許可されていますが、必須ではありません。
std::system_error から継承
メンバ関数
| エラーコードを返します。 ( std::system_error の公開メンバ関数) | |
| [virtual] |
説明文字列を返す ( std::system_error の仮想公開メンバ関数) |
std::runtime_error から継承
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 は同じ欠陥を対象としており、その解決策は std::ios_base::failure::~failure() の宣言に throw() を追加することです。この解決策は、2つの解決策の競合により適用されませんでした。
- ↑ 非スロー例外仕様は、現在、標準ライブラリ全体でグローバルに適用されています。そのため、標準ライブラリクラスのデストラクタは
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ストリームのエラーコード (enum) |