From 5383794cc93450fe9a63a1de1e8a3a3689f98dee Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sun, 8 Mar 2015 12:31:00 -0500 Subject: [PATCH] Runtime/LogicError and throwers --- include/json/value.h | 23 ++++++++++++++--------- src/lib_json/json_value.cpp | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 1cd6291..b590825 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -35,15 +35,20 @@ namespace Json { /** Base class for all exceptions we throw. */ -class Exception : public std::exception { -public: - Exception(std::string const& msg); - virtual ~Exception() throw(); - virtual char const* what() const throw(); -protected: - std::string const& msg_; - void* future_use_; -}; +class JSON_API Exception; +/** Exceptions which the user cannot easily avoid. + * + * E.g. out-of-memory, stack-overflow, malicious input + */ +class JSON_API RuntimeError; +/** Exceptions throw by JSON_ASSERT/JSON_FAIL macros. + * + * These are precondition-violations (user bugs) and internal errors (our bugs). + */ +class JSON_API LogicError; + +JSON_API void throwRuntimeError(std::string const& msg); +JSON_API void throwLogicError(std::string const& msg); /** \brief Type of the value held by a Value object. */ diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index f7d2cde..a71b0c1 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -152,17 +152,46 @@ static inline void releaseStringValue(char* value) { free(value); } namespace Json { +class JSON_API Exception : public std::exception { +public: + Exception(std::string const& msg); + virtual ~Exception() throw(); + virtual char const* what() const throw(); +protected: + std::string const& msg_; +}; +class JSON_API RuntimeError : public Exception { +public: + RuntimeError(std::string const& msg); +}; +class JSON_API LogicError : public Exception { +public: + LogicError(std::string const& msg); +}; + Exception::Exception(std::string const& msg) : msg_(msg) - , future_use_(NULL) -{ -} +{} Exception::~Exception() throw() {} char const* Exception::what() const throw() { return msg_.c_str(); } +RuntimeError::RuntimeError(std::string const& msg) + : Exception(msg) +{} +LogicError::LogicError(std::string const& msg) + : Exception(msg) +{} +void throwRuntimeError(std::string const& msg) +{ + throw RuntimeError(msg); +} +void throwLogicError(std::string const& msg) +{ + throw LogicError(msg); +} // ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////