最終更新日時(UTC):
が更新

履歴 編集

class template
<regex>

std::regex_error(C++11)

namespace std {
  class regex_error : public std::runtime_error;
}

概要

regex_errorクラスは、正規表現ライブラリ<regex>からのエラー報告として送出される、例外オブジェクトの型である。

有効ではない正規表現が入力された場合に送出される。

エラーとなった理由は、what()メンバ関数によってエラーメッセージ文字列として取得できるほか、code()メンバ関数によってregex_constants::error_type型のエラーコード値としても取得できる。

メンバ関数

基底クラスであるruntime_errorも参照のこと。

構築・破棄

名前 説明 対応バージョン
(constructor) コンストラクタ C++11
~regex_error() = default; デストラクタ C++11
regex_error& operator=(const regex_error&) = default;
regex_error& operator=(regex_error&&) = default;
代入演算子 C++11

エラー内容

名前 説明 対応バージョン
code エラーコードを取得する C++11

#include <iostream>
#include <regex>
#include <string>

std::string code_to_string(std::regex_constants::error_type e)
{
  using namespace std::regex_constants;
  switch (e) {
    case error_collate:    return "error collapse";
    case error_ctype:      return "error ctype";
    case error_escape:     return "error escape";
    case error_backref:    return "error back reference";
    case error_brack:      return "error bracket";
    case error_paren:      return "error paren";
    case error_brace:      return "error brace";
    case error_badbrace:   return "error bad brace";
    case error_range:      return "error range";
    case error_space:      return "error space";
    case error_badrepeat:  return "error bad repeat";
    case error_complexity: return "error complexity";
    case error_stack:      return "error stack";
    default:
      throw std::invalid_argument("invalid error code");
  }
}

int main()
{
  try {
    std::regex re("("); // 開きカッコに対応する閉じカッコがない正規表現を指定
  }
  catch (std::regex_error& e) {
    std::cout << code_to_string(e.code()) << std::endl;
    std::cout << e.what() << std::endl;
  }
}

出力例

error paren
The expression contained mismatched ( and ).

バージョン

言語

  • C++11

処理系