out_of_range.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_OUT_OF_RANGE_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_OUT_OF_RANGE_HPP
3 
5 #include <typeinfo>
6 #include <sstream>
7 #include <stdexcept>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Throw an out_of_range exception with a consistently formatted message.
14  *
15  * This is an abstraction for all Stan functions to use when throwing
16  * out of range. This will allow us to change the behavior for all
17  * functions at once.
18  *
19  * The message is:
20  * "<function>: index <index> out of range; expecting index to be between "
21  * "1 and <max><msg1><msg2>"
22  *
23  * @param function Name of the function
24  * @param max Max
25  * @param index Index
26  * @param msg1 Message to print. Default is "".
27  * @param msg2 Message to print. Default is "".
28  * @throw std::out_of_range with message.
29  */
30 inline void out_of_range(const char* function, int max, int index,
31  const char* msg1 = "", const char* msg2 = "") {
32  std::ostringstream message;
33 
34  message << function << ": accessing element out of range. "
35  << "index " << index << " out of range; "
36  << "expecting index to be between " << stan::error_index::value
37  << " and " << stan::error_index::value - 1 + max << msg1 << msg2;
38 
39  throw std::out_of_range(message.str());
40 }
41 
42 } // namespace math
43 } // namespace stan
44 #endif
void out_of_range(const char *function, int max, int index, const char *msg1="", const char *msg2="")
int max(const std::vector< int > &x)
Definition: max.hpp:22