C++11 §28.6 states
The class regex_error
defines the type of objects thrown as exceptions
to report errors from the regular expression library.
Which means that the <regex>
library should not throw anything else by itself. You are correct that constructing a regex_error
which inherits from runtime_error
may throw bad_alloc
during construction due to out-of-memory conditions, therefore you must also check for this in your error handling code. Unfortunately this makes it impossible to determine which regex_error
construction actually throws bad_alloc
.
For regular expressions algorithms in §28.11 it is stated in §28.11.1 that
The algorithms described in this subclause may throw an exception of type regex_error
. If such an exception e
is thrown, e.code()
shall return either regex_constants::error_complexity
or regex_-constants::error_stack
.
This means that if the functions in §28.11 ever throw a regex_error
, it shall hold one of these codes and nothing else. However, note also that things you pass to the <regex>
library, such as allocators etc might also throw, e.g. the allocator of match_results
which may trigger if results are added to the given match_results
container. Also note that §28.11 has shorthand functions which "as if" construct match_results
, such as
template <class BidirectionalIterator, class charT, class traits>
bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
const basic_regex<charT, traits> & e,
regex_constants::match_flag_type flags =
regex_constants::match_default);
template <class BidirectionalIterator, class charT, class traits>
bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
const basic_regex<charT, traits> & e,
regex_constants::match_flag_type flags =
regex_constants::match_default);
and possibly others. Since such might construct and use match_results
with the standard allocator
internally, they might throw anything std::allocator
throws. Therefore your simple example of regex_match(anyString, regex("."))
might also throw due to construction and usage of the default allocator.
Another caveat to note that for some <regex>
functions and classes it is currently impossible to determine whether a bad_alloc
was thrown by some allocator or during construction of a regex_error
exception.
In general, if you need something with a better exception specifications avoid using <regex>
. If you require simple pattern matching you're better off rolling your own safe match/search/replace functions, because it is impossible to constrain your regular expressions to avoid these exceptions in a portable nor forwards-compatible manner, even using an empty regular expression ""
might give you an exception.
PS: Note that the C++11 standard is rather poorly written in some aspects, lacking complete cross referencing. E.g. there's no explicit notice under the clauses for the methods of match_results
to throw anything, whereas §28.10.1.1 states (emphasis mine):
In all match_results
constructors, a copy of the Allocator
argument shall be used for any memory allocation performed by the constructor or member functions during the lifetime of the object.
So take care when browsing the standards like a lawyer! ;-)