rethrow_located.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_RETHROW_LOCATED_HPP
2 #define STAN_LANG_RETHROW_LOCATED_HPP
3 
5 #include <exception>
6 #include <ios>
7 #include <new>
8 #include <sstream>
9 #include <stdexcept>
10 #include <string>
11 #include <typeinfo>
12 
13 namespace stan {
14 
15  namespace lang {
16 
17  /**
18  * Returns true if the specified exception can be dynamically
19  * cast to the template parameter type.
20  *
21  * @tparam E Type to test.
22  * @param[in] e Exception to test.
23  * @return true if exception can be dynamically cast to type.
24  */
25  template <typename E>
26  bool is_type(const std::exception& e) {
27  try {
28  (void) dynamic_cast<const E&>(e);
29  return true;
30  } catch (...) {
31  return false;
32  }
33  }
34 
35  /**
36  * Structure for a located exception for standard library
37  * exception types that have no what-based constructors.
38  *
39  * @param E Type of original exception.
40  */
41  template <typename E>
42  struct located_exception : public E {
44 
45  /**
46  * Construct a located exception with no what message.
47  */
48  located_exception() throw() : what_("") { }
49 
50  /**
51  * Construct a located exception with the specified what
52  * message and specified original type.
53  *
54  * @param[in] what Original what message.
55  * @param[in] orig_type Original type.
56  */
58  const std::string& orig_type) throw()
59  : what_(what + " [origin: " + orig_type + "]") {
60  }
61 
62  /**
63  * Destroy a located exception.
64  */
65  ~located_exception() throw() { }
66 
67  /**
68  * Return the character sequence describing the exception,
69  * including the original waht message and original type if
70  * constructed with such.
71  *
72  * @return Description of exception.
73  */
74  const char* what() const throw() {
75  return what_.c_str();
76  }
77  };
78 
79  /**
80  * Rethrow an exception of type specified by the dynamic type of
81  * the specified exception, adding the specified line number to
82  * the specified exception's message.
83  *
84  * @param[in] e original exception
85  * @param[in] line line number in Stan source program where
86  * exception originated
87  * @param[in] reader trace of how program was included from files
88  */
89  inline void rethrow_located(const std::exception& e, int line,
90  const io::program_reader& reader =
92  using std::bad_alloc; // -> exception
93  using std::bad_cast; // -> exception
94  using std::bad_exception; // -> exception
95  using std::bad_typeid; // -> exception
96  using std::ios_base; // ::failure -> exception
97  using std::domain_error; // -> logic_error
98  using std::invalid_argument; // -> logic_error
99  using std::length_error; // -> logic_error
100  using std::out_of_range; // -> logic_error
101  using std::logic_error; // -> exception
102  using std::overflow_error; // -> runtime_error
103  using std::range_error; // -> runtime_error
104  using std::underflow_error; // -> runtime_error
105  using std::runtime_error; // -> exception
106  using std::exception;
107 
108  // create message with trace of includes and location of error
109  std::stringstream o;
110  o << "Exception: " << e.what();
111  if (line < 1) {
112  o << " Found before start of program.";
113  } else {
114  io::program_reader::trace_t tr = reader.trace(line);
115  o << " (in '" << tr[tr.size() - 1].first
116  << "' at line " << tr[tr.size() - 1].second;
117  for (int i = tr.size() - 1; --i >= 0; )
118  o << "; included from '" << tr[i].first
119  << "' at line " << tr[i].second;
120  o << ")" << std::endl;
121  }
122  std::string s = o.str();
123 
124  if (is_type<bad_alloc>(e))
125  throw located_exception<bad_alloc>(s, "bad_alloc");
126  if (is_type<bad_cast>(e))
127  throw located_exception<bad_cast>(s, "bad_cast");
128  if (is_type<bad_exception>(e))
129  throw located_exception<bad_exception>(s, "bad_exception");
130  if (is_type<bad_typeid>(e))
131  throw located_exception<bad_typeid>(s, "bad_typeid");
132  if (is_type<domain_error>(e))
133  throw domain_error(s);
134  if (is_type<invalid_argument>(e))
135  throw invalid_argument(s);
136  if (is_type<length_error>(e))
137  throw length_error(s);
138  if (is_type<out_of_range>(e))
139  throw out_of_range(s);
140  if (is_type<logic_error>(e))
141  throw logic_error(s);
142  if (is_type<overflow_error>(e))
143  throw overflow_error(s);
144  if (is_type<range_error>(e))
145  throw range_error(s);
146  if (is_type<underflow_error>(e))
147  throw underflow_error(s);
148  if (is_type<runtime_error>(e))
149  throw runtime_error(s);
150 
151  throw located_exception<exception>(s, "unknown original type");
152  }
153 
154  }
155 
156 }
157 
158 #endif
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
void rethrow_located(const std::exception &e, int line, const io::program_reader &reader=stan::io::program_reader())
const XML_Char * s
Definition: expat.h:262
double lang
Definition: runWimpSim.h:113
Float_t E
Definition: plot.C:20
std::vector< path_line_t > trace_t
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
void out_of_range(const char *function, int max, int index, const char *msg1="", const char *msg2="")
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
bool is_type(const std::exception &e)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
const char * what() const
located_exception(const std::string &what, const std::string &orig_type)
Float_t e
Definition: plot.C:35
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData