Changed "double fault" message.
[libsex.git] / source / Exception.cxx
blobe752c4f5cdb4b40575179600c331cd69f5896bed
1 #include <source/Exception.hxx>
2 #include <cstring> // strncpy()
4 namespace libsex {
6 Exception::Exception(const char* const message) throw()
7 : _previous(0)
9 _initMessage(message);
12 Exception::Exception(
13 const char* const message,
14 const Exception& previous) throw()
15 : _previous(0)
17 _initMessage(message);
18 try {
19 _previous = previous.clone();
20 } catch (std::bad_alloc& e) {
21 // We need to know the amount of characters
22 // that can be put into _message.
24 // Size to begin with:
25 unsigned int length = Exception::LENGTH;
26 // Substract characters written so far:
27 length -= strlen(_message);
28 // Substract nullbyte appended by strcat().
29 // Prevent underflow.
30 if (length > 0) --length;
32 // Add notice to our original message:
33 strncat(_message, "\n"
34 "std::bad_alloc while cloning Exception!",
35 length);
39 Exception::~Exception() throw()
41 if (_previous) delete _previous;
44 void Exception::write(std::ostream& out) const throw(std::ios_base::failure)
46 out << what();
49 void Exception::backtrace(std::ostream& out) const throw(std::ios_base::failure)
51 write(out);
52 if (_previous != 0) {
53 out << "\n";
54 _previous->backtrace(out);
58 const Exception* Exception::clone() const
59 throw(std::bad_alloc)
61 if (_previous) {
62 return new Exception(_message, *_previous);
63 } else {
64 return new Exception(_message);
68 const char* Exception::what() const throw()
70 return _message;
73 void Exception::_initMessage(const char* const message)
75 strncpy(_message, message, Exception::LENGTH);
76 // if |message| < |_message|
77 // strncpy() will pad with 0
78 // else
79 // strncpy() will NOT add a trailing 0!
80 _message[Exception::LENGTH] = 0;
83 } // namespace