4 * Definition of @ref libsex::Exception.
7 #include <libsex/Exception.hxx>
8 #include <cstring> // strncpy()
10 libsex::Exception::Exception(const char* const message
)
14 _initMessage(message
);
17 libsex::Exception::Exception(
18 const char* const message
,
19 const Exception
& previous
)
23 _initMessage(message
);
25 _previous
= previous
.clone();
26 } catch (std::bad_alloc
& e
) {
27 // We need to know the amount of characters
28 // that can be put into _message.
30 // Size to begin with:
31 unsigned int length
= Exception::LENGTH
;
32 // Substract characters written so far:
33 length
-= strlen(_message
);
34 // Substract nullbyte appended by strcat().
36 if (length
> 0) --length
;
38 // Add notice to our original message:
39 strncat(_message
, "\n"
40 "std::bad_alloc while cloning Exception!",
45 libsex::Exception::~Exception()
48 if (_previous
) delete _previous
;
51 void libsex::Exception::write(std::ostream
& out
) const
52 throw(std::ios_base::failure
)
57 void libsex::Exception::backtrace(std::ostream
& out
) const
58 throw(std::ios_base::failure
)
63 _previous
->backtrace(out
);
67 const libsex::Exception
* libsex::Exception::clone() const
71 return new Exception(_message
, *_previous
);
73 return new Exception(_message
);
77 const char* libsex::Exception::what() const
83 void libsex::Exception::_initMessage(const char* const message
)
85 strncpy(_message
, message
, Exception::LENGTH
);
86 // if |message| < |_message|
87 // strncpy() will pad with 0
89 // strncpy() will NOT add a trailing 0!
90 _message
[Exception::LENGTH
] = 0;