Skip to content

Commit 5383794

Browse files
committed
Runtime/LogicError and throwers
1 parent 75279cc commit 5383794

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

include/json/value.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ namespace Json {
3535

3636
/** Base class for all exceptions we throw.
3737
*/
38-
class Exception : public std::exception {
39-
public:
40-
Exception(std::string const& msg);
41-
virtual ~Exception() throw();
42-
virtual char const* what() const throw();
43-
protected:
44-
std::string const& msg_;
45-
void* future_use_;
46-
};
38+
class JSON_API Exception;
39+
/** Exceptions which the user cannot easily avoid.
40+
*
41+
* E.g. out-of-memory, stack-overflow, malicious input
42+
*/
43+
class JSON_API RuntimeError;
44+
/** Exceptions throw by JSON_ASSERT/JSON_FAIL macros.
45+
*
46+
* These are precondition-violations (user bugs) and internal errors (our bugs).
47+
*/
48+
class JSON_API LogicError;
49+
50+
JSON_API void throwRuntimeError(std::string const& msg);
51+
JSON_API void throwLogicError(std::string const& msg);
4752

4853
/** \brief Type of the value held by a Value object.
4954
*/

src/lib_json/json_value.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,46 @@ static inline void releaseStringValue(char* value) { free(value); }
152152

153153
namespace Json {
154154

155+
class JSON_API Exception : public std::exception {
156+
public:
157+
Exception(std::string const& msg);
158+
virtual ~Exception() throw();
159+
virtual char const* what() const throw();
160+
protected:
161+
std::string const& msg_;
162+
};
163+
class JSON_API RuntimeError : public Exception {
164+
public:
165+
RuntimeError(std::string const& msg);
166+
};
167+
class JSON_API LogicError : public Exception {
168+
public:
169+
LogicError(std::string const& msg);
170+
};
171+
155172
Exception::Exception(std::string const& msg)
156173
: msg_(msg)
157-
, future_use_(NULL)
158-
{
159-
}
174+
{}
160175
Exception::~Exception() throw()
161176
{}
162177
char const* Exception::what() const throw()
163178
{
164179
return msg_.c_str();
165180
}
181+
RuntimeError::RuntimeError(std::string const& msg)
182+
: Exception(msg)
183+
{}
184+
LogicError::LogicError(std::string const& msg)
185+
: Exception(msg)
186+
{}
187+
void throwRuntimeError(std::string const& msg)
188+
{
189+
throw RuntimeError(msg);
190+
}
191+
void throwLogicError(std::string const& msg)
192+
{
193+
throw LogicError(msg);
194+
}
166195

167196
// //////////////////////////////////////////////////////////////////
168197
// //////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)