libsex
3.1.0
simple exception library (C++)
|
Generic error made up of a message and (optional) references to previous one. More...
#include <Exception.hxx>
Public Member Functions | |
Exception (const char *const message) throw () | |
Creates an exception. | |
Exception (const char *const message, const Exception &previous) throw () | |
Creates a chained exception. | |
virtual | ~Exception () throw () |
No-op. | |
void | write (std::ostream &out) const throw (std::ios_base::failure) |
Writes our message to passed stream. | |
void | backtrace (std::ostream &out) const throw (std::ios_base::failure) |
Writes a backtrace to passed stream. | |
virtual const Exception * | clone () const throw (std::bad_alloc) |
Creates a shallow copy of this exception. | |
virtual const char * | what () const throw () |
Returns a pointer to the internal character string. | |
Static Public Attributes | |
static const unsigned short | LENGTH = 500 |
Max payload of the internal character string. |
Generic error made up of a message and (optional) references to previous one.
Double-fault save (not throwing std::bad_alloc or other exceptions in critical situations) and allows exception chaining.
For object-oriented error handling you should subclass this class for every type of error. There are elegant macros in
libsex::Exception::Exception | ( | const char *const | message | ) | throw () |
Creates an exception.
Supplied character array should be null- terminated and should not exceed LENGTH characters (excl. null byte). Its contents will be copied into an internal buffer and can be overwritten/freed afterwards.
The constructors do not accept std::string since that class uses the heap and may throw std::bad_alloc -- which might obviously happen even before this constructor is entered and thus might result in loss of the actual (chain of) exception(s) describing the error.
Definition at line 10 of file Exception.cxx.
libsex::Exception::Exception | ( | const char *const | message, |
const Exception & | previous | ||
) | throw () |
Creates a chained exception.
Both the error message and the previous exception are cloned.
Since exceptions should be created on the stack (see std::bad_alloc), the previous one will be internally copied to the heap by calling clone().
If clone() throws std::bad_alloc, a notice is appended to the internal message buffer of this exception (as long as space left) and std::bad_alloc is swallowed.
Definition at line 17 of file Exception.cxx.
void libsex::Exception::backtrace | ( | std::ostream & | out | ) | const throw (std::ios_base::failure) |
Writes a backtrace to passed stream.
Messages are delimited by one newline and are printed in reverse chronological order.
std::ios_base::failure | may be thrown by stream, if it is configured to do so. |
Definition at line 57 of file Exception.cxx.
const libsex::Exception * libsex::Exception::clone | ( | ) | const throw (std::bad_alloc) [virtual] |
Creates a shallow copy of this exception.
Copy is allocated on the heap.
std::bad_alloc | on memory shortage. |
Definition at line 67 of file Exception.cxx.
void libsex::Exception::write | ( | std::ostream & | out | ) | const throw (std::ios_base::failure) |
Writes our message to passed stream.
std::ios_base::failure | may be thrown by stream, if it is configured to do so. |
Definition at line 51 of file Exception.cxx.