aboutsummaryrefslogtreecommitdiff
path: root/src/exceptionbase.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/exceptionbase.h')
-rw-r--r--src/exceptionbase.h114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/exceptionbase.h b/src/exceptionbase.h
new file mode 100644
index 0000000..fd78089
--- /dev/null
+++ b/src/exceptionbase.h
@@ -0,0 +1,114 @@
1#ifndef EXCEPTION_BASE_H
2#define EXCEPTION_BASE_H
3
4#include <string>
5#include <exception>
6#include <stdarg.h>
7
8namespace Bu
9{
10 /**
11 * A generalized Exception base class. This is nice for making general and
12 * flexible child classes that can create new error code classes.
13 *
14 * In order to create your own exception class use these two lines.
15 *
16 * in your header: subExceptionDecl( NewClassName );
17 *
18 * in your source: subExcpetienDef( NewClassName );
19 */
20 class ExceptionBase : public std::exception
21 {
22 public:
23 /**
24 * Construct an exception with an error code of zero, but with a
25 * description. The use of this is not reccomended most of the time,
26 * it's generally best to include an error code with the exception so
27 * your program can handle the exception in a better way.
28 * @param sFormat The format of the text. See printf for more info.
29 */
30 ExceptionBase( const char *sFormat, ... ) throw();
31
32 /**
33 *
34 * @param nCode
35 * @param sFormat
36 */
37 ExceptionBase( int nCode, const char *sFormat, ... ) throw();
38
39 /**
40 *
41 * @param nCode
42 * @return
43 */
44 ExceptionBase( int nCode=0 ) throw();
45
46 /**
47 *
48 * @return
49 */
50 virtual ~ExceptionBase() throw();
51
52 /**
53 *
54 * @return
55 */
56 virtual const char *what() const throw();
57
58 /**
59 *
60 * @return
61 */
62 int getErrorCode();
63
64 /**
65 *
66 * @param lpFormat
67 * @param vargs
68 */
69 void setWhat( const char *lpFormat, va_list &vargs );
70
71 /**
72 *
73 * @param lpText
74 */
75 void setWhat( const char *lpText );
76
77 private:
78 int nErrorCode; /**< The code for the error that occured. */
79 char *sWhat; /**< The text string telling people what went wrong. */
80 };
81}
82
83#define subExceptionDecl( name ) \
84class name : public ExceptionBase \
85{ \
86 public: \
87 name( const char *sFormat, ... ) throw (); \
88 name( int nCode, const char *sFormat, ... ) throw(); \
89 name( int nCode=0 ) throw (); \
90};
91
92#define subExceptionDef( name ) \
93name::name( const char *lpFormat, ... ) throw() : \
94 ExceptionBase( 0 ) \
95{ \
96 va_list ap; \
97 va_start( ap, lpFormat ); \
98 setWhat( lpFormat, ap ); \
99 va_end( ap ); \
100} \
101name::name( int nCode, const char *lpFormat, ... ) throw() : \
102 ExceptionBase( nCode ) \
103{ \
104 va_list ap; \
105 va_start( ap, lpFormat ); \
106 setWhat( lpFormat, ap ); \
107 va_end( ap ); \
108} \
109name::name( int nCode ) throw() : \
110 ExceptionBase( nCode ) \
111{ \
112}
113
114#endif