std::regex_error - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::regex_error

From cppreference.com
< cpp‎ | regex

Defined in header `<regex>` |  |
class regex_error : public std::runtime_error { public: explicit regex_error(std::regex_constants::error_type ecode); std::regex_constants::error_type code() const; }; |  | (since C++11)

Defines the type of exception object thrown to report errors in the regular expressions library.

Inheritance diagram

### [edit] Member functions

(constructor) | constructs a `regex_error` object (public member function) [edit]
code | gets the std::regex_constants::error_type for a `regex_error` (public member function) [edit]

## Inherited from std::exception

### Member functions

(destructor) [virtual] | destructs the exception object (virtual public member function of `std::exception`) [edit]
what [virtual] | returns an explanatory string (virtual public member function of `std::exception`) [edit]

### [edit] Example

Run this code

    #include <regex>
    #include <iostream>

    int main()
    {
        try {
            std::regex re("[a-b][a");
        }

        catch (const std::regex_error& e) {
            std::cout << "regex_error caught: " << e.what() << '\n';
            if (e.code() == std::regex_constants::error_brack) {
                std::cout << "The code was error_brack\n";
            }
        }
    }

Output:

    regex_error caught: The expression contained mismatched [ and ].
    The code was error_brack

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/regex/regex_error&oldid=63421"