1 #ifndef STAN_MATH_PRIM_SCAL_ERR_INVALID_ARGUMENT_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_INVALID_ARGUMENT_HPP
3 
4 #include <typeinfo>
5 #include <sstream>
6 #include <stdexcept>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  * Throw an invalid_argument exception with a consistently formatted message.
13  *
14  * This is an abstraction for all Stan functions to use when throwing
15  * invalid argument. This will allow us to change the behavior for all
16  * functions at once.
17  *
18  * The message is:
19  * "<function>: <name> <msg1><y><msg2>"
20  *
21  * @tparam T Type of variable
22  * @param function Name of the function
23  * @param name Name of the variable
24  * @param y Variable
25  * @param msg1 Message to print before the variable
26  * @param msg2 Message to print after the variable
27  * @throw std::invalid_argument
28  */
29 template <typename T>
invalid_argument(const char * function,const char * name,const T & y,const char * msg1,const char * msg2)30 inline void invalid_argument(const char* function, const char* name, const T& y,
31                              const char* msg1, const char* msg2) {
32   std::ostringstream message;
33 
34   message << function << ": " << name << " " << msg1 << y << msg2;
35 
36   throw std::invalid_argument(message.str());
37 }
38 
39 /**
40  * Throw an invalid_argument exception with a consistently formatted message.
41  *
42  * This is an abstraction for all Stan functions to use when throwing
43  * invalid argument. This will allow us to change the behavior for all
44  * functions at once. (We've already changed behavior mulitple times up
45  * to Stan v2.5.0.)
46  *
47  * The message is:
48  * "<function>: <name> <msg1><y>"
49  *
50  * @tparam T Type of variable
51  * @param function Name of the function
52  * @param name Name of the variable
53  * @param y Variable
54  * @param msg1 Message to print before the variable
55  * @throw std::invalid_argument
56  */
57 template <typename T>
invalid_argument(const char * function,const char * name,const T & y,const char * msg1)58 inline void invalid_argument(const char* function, const char* name, const T& y,
59                              const char* msg1) {
60   invalid_argument(function, name, y, msg1, "");
61 }
62 
63 }  // namespace math
64 }  // namespace stan
65 #endif
66