1
0
Fork 0

Use references when catching polymorphic exceptions

This commit is contained in:
krcroft 2020-03-31 14:40:19 -07:00 committed by Patryk Obara
parent 3aa27e0491
commit 73e22747e7

View file

@ -46,6 +46,7 @@
*/
template<typename T>
T to_finite(const std::string& input) {
// Defensively set NaN from the get-go
T result = std::numeric_limits<T>::quiet_NaN();
size_t bytes_read = 0;
try {
@ -53,9 +54,9 @@ T to_finite(const std::string& input) {
if (!input.empty() && bytes_read == input.size())
result = static_cast<T>(interim);
}
// handle exceptions that stod may throw
catch (std::invalid_argument e) {}
catch (std::out_of_range e) {}
// Capture expected exceptions stod may throw
catch (std::invalid_argument &e) {}
catch (std::out_of_range &e) {}
return result;
}