1 #ifndef LIBSEX_EXCEPTION_HXX
2 #define LIBSEX_EXCEPTION_HXX
4 #include <exception> // std::exception
5 #include <stdexcept> // std::bad_alloc
6 #include <ostream> // std::ostream
11 * Generic error made up of a message and
12 * (optional) references to previous one.
14 * Double-fault save (not throwing std::bad_alloc
15 * or other exceptions in critical situations) and
16 * allows exception chaining.
18 * For object-oriented error handling you should
19 * subclass this class for every type of error.
20 * There are elegant macros in @file declare.hxx
21 * and @file define.hxx.
26 class libsex::Exception
: public std::exception
29 /// Max payload of the internal character string.
30 static const unsigned short LENGTH
= 500;
33 * Creates an exception.
35 * Supplied character array should be null-
36 * terminated and should not exceed LENGTH
37 * characters (excl. null byte). Its contents will
38 * be copied into an internal buffer and can be
39 * overwritten/freed afterwards.
41 * The constructors do not accept std::string
42 * since that class uses the heap and may throw
43 * std::bad_alloc -- which might obviously happen
44 * even before this constructor is entered and
45 * thus might result in loss of the actual (chain
46 * of) exception(s) describing the error.
48 Exception(const char* const message
) throw();
51 * Creates a chained exception.
53 * Both the error message and the previous
54 * exception are cloned.
56 * Since exceptions should be created on the stack
57 * (see std::bad_alloc), the previous one will be
58 * internally copied to the heap by calling
61 * If clone() throws std::bad_alloc, a notice is
62 * appended to the internal message buffer of this
63 * exception (as long as space left) and
64 * std::bad_alloc is swallowed.
66 * @see Exception(message)
69 const char* const message
,
70 const Exception
& previous
) throw();
73 virtual ~Exception() throw();
76 * Writes our message to passed stream.
78 * @exception std::ios_base::failure
79 * may be thrown by stream, if it is
80 * configured to do so.
82 void write(std::ostream
& out
) const throw(std::ios_base::failure
);
85 * Writes a backtrace to passed stream.
87 * Messages are delimited by one newline and are
88 * printed in reverse chronological order.
90 * @exception std::ios_base::failure
91 * may be thrown by stream, if it is
92 * configured to do so.
94 void backtrace(std::ostream
& out
) const throw(std::ios_base::failure
);
97 * Creates a shallow copy of this exception.
99 * Copy is allocated on the heap.
101 * @throws std::bad_alloc on memory shortage.
103 virtual const Exception
* clone() const throw(std::bad_alloc
);
105 /// Returns a pointer to the internal character string.
106 virtual const char* what() const throw();
110 * Copy of message passed to constructor.
112 * Length is @ref Exception::LENGTH plus nullbyte.
114 char _message
[LENGTH
+ 1];
116 /// Pointer to copy of previous exception, or null.
117 const Exception
* _previous
;
119 /// Used by constructors to initialize _message.
120 void _initMessage(const char* const message
);