From 4c05636ff0cfcc99e86ed16ac4e55fc2c2a46450 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Fri, 25 Sep 2015 00:13:30 +0200 Subject: [PATCH] Improved exception handling --- doorlockd/src/util.cpp | 12 ++++++------ doorlockd/src/util.h | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/doorlockd/src/util.cpp b/doorlockd/src/util.cpp index 822955b..7e8bab9 100644 --- a/doorlockd/src/util.cpp +++ b/doorlockd/src/util.cpp @@ -8,7 +8,7 @@ int getJson(const Json::Value &root, const string &key) auto val = root.get(key, Json::Value()); if (val.isInt()) return val.asInt(); - throw "Json Type error"; + throw std::runtime_error("Json Type error"); } template <> @@ -17,7 +17,7 @@ string getJson(const Json::Value &root, const string &key) auto val = root.get(key, Json::Value()); if (val.isString()) return val.asString(); - throw "Json Type error"; + throw std::runtime_error("Json Type error"); } template <> @@ -26,7 +26,7 @@ size_t getJson(const Json::Value &root, const string &key) auto val = root.get(key, Json::Value()); if (val.isInt()) return val.asUInt64(); - throw "Json Type error"; + throw std::runtime_error("Json Type error"); } template <> @@ -35,7 +35,7 @@ bool getJson(const Json::Value &root, const string &key) auto val = root.get(key, Json::Value()); if (val.isBool()) return val.asBool(); - throw "Json Type error"; + throw std::runtime_error("Json Type error"); } template <> @@ -90,14 +90,14 @@ unsigned char hex2uchar(const char input) } else if(input >= 'a' && input <= 'f') { return input - 'a' + 10; } - throw("Malformed hexadecimal input"); + throw std::runtime_error("Malformed hexadecimal input"); } uint64_t toUint64(const string &s) { if (s.length() != (64/4)) { - throw("Hex string has invalid length"); + throw std::runtime_error("Hex string has invalid length"); } uint64_t retval = 0; diff --git a/doorlockd/src/util.h b/doorlockd/src/util.h index d9a8026..9779ff5 100644 --- a/doorlockd/src/util.h +++ b/doorlockd/src/util.h @@ -2,6 +2,7 @@ #define UTIL_H #include +#include #include template @@ -13,7 +14,7 @@ T getJsonOrFail(const Json::Value &root, const std::string &key) const auto members = root.getMemberNames(); if (std::find(members.begin(), members.end(), key) == members.end()) { - throw "Json key not existing"; + throw std::runtime_error("Json key not existing"); } return getJson(root, key);