00001
00002
00003 #ifndef BASE_EXCEPTION_H
00004 #define BASE_EXCEPTION_H
00005
00006 #include <cstdarg>
00007 #include "String.h"
00008
00009 namespace base {
00011
00015 class PPBASE_EXPORT Exception: public std::exception {
00016 public:
00018
00022 explicit Exception(const char *fmt, ...)
00023 #ifdef __GNUC__
00024 __attribute__((format(printf, 2, 3)))
00025 #endif
00026 ;
00027
00029 virtual ~Exception() throw();
00030
00032 void appendMessage(const String &msg);
00033
00035
00038 int getErrcode() const;
00039
00041
00044 const String &getLocation() const;
00045
00047
00050 const String &getMessage() const;
00051
00053
00056 const String &getNested() const;
00057
00059
00062 const String &getStacktrace() const;
00063
00065
00068 const String &getType() const;
00069
00071
00074 virtual String toString() const;
00075
00077
00081 static void trace(String &location, String &stacktrace);
00082
00084
00088 const char *what() const throw();
00089
00091
00100 Exception &operator=(const Exception &ex);
00101
00102 protected:
00104
00108 Exception(int errcode, const char *type);
00109
00110 int _errcode;
00111 String _location;
00112 String _message;
00113 String _nested;
00114 String _stacktrace;
00115 String _type;
00116 };
00117
00119
00124 PPBASE_EXPORT std::ostream &operator<<(std::ostream &out, const Exception &ex);
00125 }
00126
00127 #define EXCEPTION_CONSTRUCTOR_DEFAULT(ExceptionClass) \
00128 ExceptionClass::ExceptionClass(int errcode, const char *fmt, ...): Exception(errcode, #ExceptionClass) { \
00129 va_list ap; \
00130 \
00131 if (fmt) { \
00132 va_start(ap, fmt); \
00133 _message.assignvf(fmt, ap); \
00134 va_end(ap); \
00135 } \
00136 }
00137
00138 #define EXCEPTION_CONSTRUCTOR_NESTED(ExceptionClass) \
00139 ExceptionClass::ExceptionClass(int errcode, const Exception &ex, const char *fmt, ...): Exception(errcode, #ExceptionClass) { \
00140 va_list ap; \
00141 \
00142 if (fmt) { \
00143 va_start(ap, fmt); \
00144 _message.assignvf(fmt, ap); \
00145 va_end(ap); \
00146 } \
00147 _nested = ex.toString(); \
00148 }
00149
00150 #endif