Stan  2.10.0
probability, sampling & optimization
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 
4 #include <exception>
5 #include <ios>
6 #include <new>
7 #include <sstream>
8 #include <stdexcept>
9 #include <string>
10 #include <typeinfo>
11 
12 namespace stan {
13 
14  namespace lang {
15 
24  template <typename E>
25  bool is_type(const std::exception& e) {
26  try {
27  (void) dynamic_cast<const E&>(e);
28  return true;
29  } catch (...) {
30  return false;
31  }
32  }
33 
40  template <typename E>
41  struct located_exception : public E {
42  std::string what_;
43 
47  located_exception() throw() : what_("") { }
48 
56  located_exception(const std::string& what,
57  const std::string& orig_type) throw()
58  : what_(what + " [origin: " + orig_type + "]") {
59  }
60 
64  ~located_exception() throw() { }
65 
73  const char* what() const throw() {
74  return what_.c_str();
75  }
76  };
77 
87  void rethrow_located(const std::exception& e, int line) {
88  using std::bad_alloc; // -> exception
89  using std::bad_cast; // -> exception
90  using std::bad_exception; // -> exception
91  using std::bad_typeid; // -> exception
92  using std::ios_base; // ::failure -> exception
93 
94  using std::domain_error; // -> logic_error
95  using std::invalid_argument; // -> logic_error
96  using std::length_error; // -> logic_error
97  using std::out_of_range; // -> logic_error
98  using std::logic_error; // -> exception
99 
100  using std::overflow_error; // -> runtime_error
101  using std::range_error; // -> runtime_error
102  using std::underflow_error; // -> runtime_error
103  using std::runtime_error; // -> exception
104 
105  using std::exception;
106 
107  std::stringstream o;
108  o << "Exception thrown at line " << line << ": "
109  << e.what();
110  std::string s = o.str();
111 
112  if (is_type<bad_alloc>(e))
113  throw located_exception<bad_alloc>(s, "bad_alloc");
114  if (is_type<bad_cast>(e))
115  throw located_exception<bad_cast>(s, "bad_cast");
116  if (is_type<bad_exception>(e))
117  throw located_exception<bad_exception>(s, "bad_exception");
118  if (is_type<bad_typeid>(e))
119  throw located_exception<bad_typeid>(s, "bad_typeid");
120  if (is_type<std::ios_base::failure>(e))
121  throw std::ios_base::failure(s);
122  if (is_type<domain_error>(e))
123  throw domain_error(s);
124  if (is_type<invalid_argument>(e))
125  throw invalid_argument(s);
126  if (is_type<length_error>(e))
127  throw length_error(s);
128  if (is_type<out_of_range>(e))
129  throw out_of_range(s);
130  if (is_type<logic_error>(e))
131  throw logic_error(s);
132  if (is_type<overflow_error>(e))
133  throw overflow_error(s);
134  if (is_type<range_error>(e))
135  throw range_error(s);
136  if (is_type<underflow_error>(e))
137  throw underflow_error(s);
138  if (is_type<runtime_error>(e))
139  throw runtime_error(s);
140 
141  throw located_exception<exception>(s, "unknown original type");
142  }
143 
144  }
145 
146 }
147 
148 #endif
Structure for a located exception for standard library exception types that have no what-based constr...
Probability, optimization and sampling library.
located_exception()
Construct a located exception with no what message.
~located_exception()
Destroy a located exception.
bool is_type(const std::exception &e)
Returns true if the specified exception can be dynamically cast to the template parameter type...
const char * what() const
Return the character sequence describing the exception, including the original waht message and origi...
void rethrow_located(const std::exception &e, int line)
Rethrow an exception of type specified by the dynamic type of the specified exception, adding the specified line number to the specified exception's message.
located_exception(const std::string &what, const std::string &orig_type)
Construct a located exception with the specified what message and specified original type...

     [ Stan Home Page ] © 2011–2016, Stan Development Team.