casadi_exception.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010 by Joel Andersson, Moritz Diehl, K.U.Leuven. All rights reserved.
6  *
7  * CasADi is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 3 of the License, or (at your option) any later version.
11  *
12  * CasADi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with CasADi; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 
23 #ifndef CASADI_EXCEPTION_HPP
24 #define CASADI_EXCEPTION_HPP
25 
26 #include <exception>
27 #include <string>
28 #include <sstream>
29 #include <iostream>
30 #include <stdexcept>
31 
32 namespace CasADi{
33 
50 class CasadiException : public std::exception{
51  public:
54  }
55 
57  explicit CasadiException(const std::string& msg) : msg_(msg){}
58 
60  ~CasadiException() throw(){}
61 
63  virtual const char* what() const throw(){
64  return msg_.c_str();
65  }
66 
68  CasadiException& operator<<(const std::string& msg){
69  msg_ += msg;
70  return *this;
71  }
72 
74  CasadiException& operator<<(const std::exception& ex){
75  msg_ += " => ";
76  msg_ += ex.what();
77  return *this;
78  }
79 
80  protected:
81  std::string msg_;
82 };
83 
84 
85 // Assertion similar to the standard C assert statement, with the difference that it throws an exception with the same information
86 #ifdef CASADI_NDEBUG
87 // Release mode
88 #define casadi_assert(x)
89 #define casadi_assert_message(x,msg)
90 #define casadi_assert_warning(x,msg)
91 #define casadi_warning(msg)
92 #define casadi_error(msg)
93 
94 #else // CASADI_NDEBUG
95 // Debug mode
96 // Convert to string
97 #define CASADI_ASSERT_STR1(x) #x
98 #define CASADI_ASSERT_STR(x) CASADI_ASSERT_STR1(x)
99 
100 // String denoting where the assertation is situated
101 #define CASADI_ASSERT_WHERE " on line " CASADI_ASSERT_STR(__LINE__) " of file " CASADI_ASSERT_STR(__FILE__)
102 
103 #define casadi_log(msg) \
104  if(verbose()){ \
105  std::cout << "CasADi log message: " << msg << std::endl; \
106  }
107 
108 #define casadi_error(msg) \
109  {\
110  std::stringstream ss_internal_; \
111  ss_internal_ << CASADI_ASSERT_WHERE << std::endl << msg << std::endl; \
112  throw CasADi::CasadiException(ss_internal_.str()); \
113  }
114 
115 // This assertion checks for illegal user inputs. It will not be checked if CASADI_NDEBUG is defined
116 #define casadi_assert_message(x,msg) \
117 { \
118  bool is_ok; \
119  try{ \
120  is_ok = x; \
121  } catch(std::exception& ex){ \
122  throw CasADi::CasadiException(std::string("When trying to check the assertion \"" CASADI_ASSERT_STR(x) "\"" CASADI_ASSERT_WHERE ", caught: \n")+ex.what());\
123  } \
124  if(!is_ok) { \
125  std::stringstream ss_internal_; \
126  ss_internal_ << "The assertion \"" CASADI_ASSERT_STR(x) "\"" CASADI_ASSERT_WHERE " failed. " << std::endl << msg << std::endl; \
127  throw CasADi::CasadiException(ss_internal_.str()); \
128  }\
129 } \
130 
131 // This assersion if for errors caused by bugs in CasADi, use it instead of C:s assert(), but never in destructors
132 #define casadi_assert(x) casadi_assert_message(x,"(Hint for developers: CasadiOptions.setCatchErrorsPython(False) to obtain gdb stacktrace in python.)" << std::endl << "Please notify the CasADi developers.")
133 
134 // This is for warnings to be issued when casadi is not in release mode and an assertion fails
135 #define casadi_assert_warning(x,msg) \
136 if((x)==false){ \
137  std::cerr << "CasADi warning: \"" << msg << "\" (assertion \"" CASADI_ASSERT_STR(x) "\"" CASADI_ASSERT_WHERE " failed.)" << std::endl;\
138 }
139 
140 // This is for warnings to be issued when casadi is not in release mode
141 #define casadi_warning(msg) \
142 std::cerr << "CasADi warning: \"" << msg << "\" issued " CASADI_ASSERT_WHERE ". " << std::endl;
143 
144 #endif // CASADI_NDEBUG
145 
146 } // namespace CasADi
147 
148 #endif // CASADI_EXCEPTION_HPP
CasadiException()
Default constructor.
Casadi exception class.
CasadiException & operator<<(const std::exception &ex)
Append an exception.
virtual const char * what() const
Display error.
CasadiException & operator<<(const std::string &msg)
Append a message.
CasadiException(const std::string &msg)
Form message string.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:29