- system_error[meta header]
- std[meta namespace]
- class[meta id-type]
- cpp11[meta cpp]
namespace std {
class error_code;
}
error_code
は、OSのAPIで発生するエラー値およびそのエラーメッセージを扱うクラスである。
このクラスは主に、system_error
例外クラスに付加する情報として使用する。
名前 |
説明 |
対応バージョン |
operator== |
等値比較 |
C++11 |
operator!= |
非等値比較 (C++20からoperator== により使用可能) |
C++11 |
operator<=> |
三方比較 |
C++20 |
operator< |
左辺が右辺より小さいか判定する (C++20からoperator<=> により使用可能) |
C++11 |
bool operator<=(const error_code&, const error_code&) noexcept; |
左辺が右辺以下か判定する (operator<=> により使用可能) |
C++20 |
bool operator>(const error_code&, const error_code&) noexcept; |
左辺が右辺より大きいか判定する (operator<=> により使用可能) |
C++20 |
bool operator>=(const error_code&, const error_code&) noexcept; |
左辺が右辺以上か判定する (operator<=> により使用可能) |
C++20 |
operator<< |
ストリームへ出力 |
C++11 |
make_error_code |
errc からerror_code オブジェクトを生成する |
C++11 |
名前 |
説明 |
対応バージョン |
hash |
error_code での特殊化 |
C++11 |
#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();
// エラー値とメッセージを出力
std::cout << ec.value() << std::endl;
std::cout << ec.message() << std::endl;
}
}
- std::error_code[color ff0000]
- std::errc::invalid_argument[link errc.md]
- std::generic_category()[link generic_category.md]
- std::system_error[link system_error.md]
- ec.value()[link error_code/value.md]
- ec.message()[link error_code/message.md]