- system_error[meta header]
- std[meta namespace]
- class[meta id-type]
- cpp11[meta cpp]
namespace std {
class error_condition;
}
error_conditionは、error_codeに紐付くエラーを表現することを可能にするためのクラスである。
Visual C++ 2010、GCC 4.6.1ではgeneric_category()とsystem_category()のerror_categoryオブジェクトはname()メンバ関数を除いて同じ挙動を行い、それぞれのdefault_error_condition()メンバ関数も同じエラー値、同じカテゴリのerror_conditionを構築するため、実質error_codeとerror_conditionは標準カテゴリでは等価な動作をする。だが、error_categoryを継承した新たなカテゴリを定義することにより、以下のようなエラーを表現することが可能となる:
| 名前 |
説明 |
対応バージョン |
operator== |
等値比較 |
C++11 |
operator!= |
非等値比較 (C++20からoperator==により使用可能) |
C++11 |
operator<=> |
三方比較 |
C++20 |
operator< |
左辺が右辺より小さいか判定する (C++20からoperator<=>により使用可能) |
C++11 |
bool operator<=(const error_condition&, const error_condition&) noexcept; |
左辺が右辺以下か判定する (operator<=>により使用可能) |
C++20 |
bool operator>(const error_condition&, const error_condition&) noexcept; |
左辺が右辺より大きいか判定する (operator<=>により使用可能) |
C++20 |
bool operator>=(const error_condition&, const error_condition&) noexcept; |
左辺が右辺以上か判定する (operator<=>により使用可能) |
C++20 |
make_error_condition |
errcからerror_conditionオブジェクトを生成する |
C++11 |
| 名前 |
説明 |
対応バージョン |
hash |
error_conditionでの特殊化 |
C++17 |
#include <iostream>
#include <system_error>
int main()
{
try {
// 不正な引数エラー
std::error_code ec(static_cast<int>(std::errc::invalid_argument),
std::generic_category());
throw std::system_error(ec, "system error!");
}
catch (std::system_error& e) {
// 例外オブジェクトからerror_codeを取得
const std::error_code& ec = e.code();
// error_codeからerror_conditionを取得
const std::error_condition& cond = ec.default_error_condition();
// エラー値とメッセージを出力
std::cout << cond.value() << std::endl;
std::cout << cond.message() << std::endl;
}
}
- std::error_condition[color ff0000]
- std::errc::invalid_argument[link errc.md]
- std::generic_category()[link generic_category.md]
- std::system_error[link system_error.md]
- ec.default_error_condition()[link error_code/default_error_condition.md]
- cond.value()[link error_condition/value.md]
- cond.message()[link error_condition/message.md]