diff options
Diffstat (limited to 'error.h')
-rw-r--r-- | error.h | 64 |
1 files changed, 36 insertions, 28 deletions
@@ -2,17 +2,20 @@ #define _ERROR_H #include <exception> +#include <stdexcept> #include <string> #include <cstring> #include <sstream> #include <list> +namespace ledger { + class error_context { - public: - std::string desc; +public: + string desc; - error_context(const std::string& _desc) throw() : desc(_desc) {} + error_context(const string& _desc) throw() : desc(_desc) {} virtual ~error_context() throw() {} virtual void describe(std::ostream& out) const throw() { if (! desc.empty()) @@ -23,11 +26,11 @@ class error_context class file_context : public error_context { protected: - std::string file; + path file; unsigned long line; public: - file_context(const std::string& _file, unsigned long _line, - const std::string& desc = "") throw() + file_context(const path& _file, unsigned long _line, + const string& desc = "") throw() : error_context(desc), file(_file), line(_line) {} virtual ~file_context() throw() {} @@ -39,13 +42,14 @@ class file_context : public error_context } }; -class line_context : public error_context { - public: - std::string line; +class line_context : public error_context +{ +public: + string line; long pos; - line_context(const std::string& _line, long _pos, - const std::string& desc = "") throw() + line_context(const string& _line, long _pos, + const string& desc = "") throw() : error_context(desc), line(_line), pos(_pos) {} virtual ~line_context() throw() {} @@ -63,15 +67,14 @@ class line_context : public error_context { ////////////////////////////////////////////////////////////////////// -class str_exception : public std::exception { - protected: - std::string reason; - public: +class str_exception : public std::logic_error +{ +public: std::list<error_context *> context; - str_exception(const std::string& _reason, + str_exception(const string& why, error_context * ctxt = NULL) throw() - : reason(_reason) { + : std::logic_error(why) { if (ctxt) context.push_back(ctxt); } @@ -80,11 +83,11 @@ class str_exception : public std::exception { for (std::list<error_context *>::iterator i = context.begin(); i != context.end(); i++) - delete *i; + checked_delete(*i); } virtual void reveal_context(std::ostream& out, - const std::string& kind) const throw() { + const string& kind) const throw() { for (std::list<error_context *>::const_reverse_iterator i = context.rbegin(); i != context.rend(); @@ -95,31 +98,36 @@ class str_exception : public std::exception { (*i)->describe(out); } } +}; - virtual const char* what() const throw() { - return reason.c_str(); +#define DECLARE_EXCEPTION(kind, name) \ + class name : public kind { \ + public: \ + name(const string& why, error_context * ctxt = NULL) throw() \ + : kind(why, ctxt) {} \ } -}; class error : public str_exception { public: - error(const std::string& reason, error_context * ctxt = NULL) throw() - : str_exception(reason, ctxt) {} + error(const string& why, error_context * ctxt = NULL) throw() + : str_exception(why, ctxt) {} virtual ~error() throw() {} }; class fatal : public str_exception { public: - fatal(const std::string& reason, error_context * ctxt = NULL) throw() - : str_exception(reason, ctxt) {} + fatal(const string& why, error_context * ctxt = NULL) throw() + : str_exception(why, ctxt) {} virtual ~fatal() throw() {} }; class fatal_assert : public fatal { public: - fatal_assert(const std::string& reason, error_context * ctxt = NULL) throw() - : fatal(std::string("assertion failed '") + reason + "'", ctxt) {} + fatal_assert(const string& why, error_context * ctxt = NULL) throw() + : fatal(string("assertion failed '") + why + "'", ctxt) {} virtual ~fatal_assert() throw() {} }; +} // namespace ledger + #endif // _ERROR_H |