3 * Author: Lukas Krejci <krejci.l@centrum.cz>, (C) 2008
4 * Copyright: See COPYING file that comes with this distribution
7 #ifndef BEACON_EXCEPTIONS_H
8 #define BEACON_EXCEPTIONS_H
16 #include <boost/bind.hpp>
22 struct exception_cause
{
23 exception_cause(char const * mes
, char const * tn
) : message(mes
), type_name(tn
) {}
26 char const * type_name
;
31 * Base class for all exceptions thrown in beacon library.
33 class exception
: public std::exception
{
36 explicit exception() throw() {}
37 explicit exception(string
const & message
) throw() : _message(message
) {}
38 explicit exception(exception
const & cause
) throw() {
42 explicit exception(string
const & message
, exception
const & cause
) throw() : _message(message
) {
46 ~exception() throw() {}
48 string
const message() const throw() {
52 char const * what() const throw() {
53 return _message
.c_str();
56 vector
<exception_cause
> const causes() const throw () {
57 return vector
<exception_cause
>(_causes
);
60 void print_stack_trace() {
61 print_stack_trace(std::cerr
);
64 void print_stack_trace(std::ostream
& os
) {
65 os
<< typeid(*this).name() << " : " << what() << endl
;
66 os
<< "Caused by:" << endl
;
67 for(vector
<exception_cause
>::iterator i
= _causes
.begin(); i
!= _causes
.end(); ++i
) {
68 exception_cause
& cause
= *i
;
69 os
<< cause
.type_name
<< " : " << cause
.message
<< endl
;
74 void init_causes(exception
const & ex
) {
75 type_info
const & t
= typeid(ex
);
76 _causes
.push_back(exception_cause(ex
.message().c_str(), t
.name()));
77 for_each(ex
.causes().begin(), ex
.causes().end(),
78 boost::bind(&vector
<exception_cause
>::push_back
, _causes
, _1
));
82 vector
<exception_cause
> _causes
;
86 * Thrown when some asynchronous operation times out.
88 class timeout_exception
: public exception
91 timeout_exception(std::string message
, exception cause
) : exception(message
, cause
) {}
92 timeout_exception(exception
const& cause
) : exception(cause
) {}
93 timeout_exception(std::string message
) : exception(message
) {}
94 timeout_exception() {}
98 * Thrown when some asynchronous operation fails.
100 class execution_exception
: public exception
103 execution_exception(std::string message
, exception cause
) : exception(message
, cause
) {}
104 execution_exception(exception
const& cause
) : exception(cause
) {}
105 execution_exception(std::string message
) : exception(message
) {}
106 execution_exception() {}