From 73e22747e7ceb0ecd9e1ae66e4baaa2865d1778a Mon Sep 17 00:00:00 2001 From: krcroft Date: Tue, 31 Mar 2020 14:40:19 -0700 Subject: [PATCH] Use references when catching polymorphic exceptions --- include/support.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/support.h b/include/support.h index 646c4117..ce0944af 100644 --- a/include/support.h +++ b/include/support.h @@ -46,6 +46,7 @@ */ template T to_finite(const std::string& input) { + // Defensively set NaN from the get-go T result = std::numeric_limits::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(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; }