Reputation: 1421
std::regex regex("*", std::regex_constants::icase);
This will throw an exception. If I have exceptions disabled, it will terminate the application, and that means I cannot catch it and do nothing with it like I normally would do.
Is there anyway to prevent invalid regex from throwing exceptions when it is constructed? Or some kind of std::regex::isvalid("*")
check that exists?
I was thinking maybe I could come up with a separate regex that parses the input regex expression string before I attempt the constructor, but I would much prefer an alternative.
Upvotes: 3
Views: 874
Reputation: 5729
You can't. There's no standard way to do it. Exceptions are an intrinsic part of the standard library, and even interfaces that don't throw their own exceptions (such as the infamous error_code
-based interface for <filesystem>
) can throw exceptions for other operations.
Your best bet is using a library that is designed to be exception-free, such as Boost.Regex with the no_except
flag enabled. You can then check for errors after construction of a boost::basic_regex
, using the member function status.
Upvotes: 2