IpException.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (C) 2004, 2006 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpException.hpp 2023 2011-06-18 18:49:49Z stefan $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPEXCEPTION_HPP__
  9. #define __IPEXCEPTION_HPP__
  10. #include "IpUtils.hpp"
  11. #include "IpJournalist.hpp"
  12. /* This file contains a base class for all exceptions
  13. * and a set of macros to help with exceptions
  14. */
  15. namespace Ipopt
  16. {
  17. /** This is the base class for all exceptions. The easiest way to
  18. * use this class is by means of the following macros:
  19. *
  20. * \verbatim
  21. DECLARE_STD_EXCEPTION(ExceptionType);
  22. \endverbatim
  23. *
  24. * This macro defines a new class with the name ExceptionType,
  25. * inherited from the base class IpoptException. After this,
  26. * exceptions of this type can be thrown using
  27. *
  28. * \verbatim
  29. THROW_EXCEPTION(ExceptionType, Message);
  30. \endverbatim
  31. *
  32. * where Message is a std::string with a message that gives an
  33. * indication of what caused the exception. Exceptions can also be
  34. * thrown using the macro
  35. *
  36. * \verbatim
  37. ASSERT_EXCEPTION(Condition, ExceptionType, Message);
  38. \endverbatim
  39. *
  40. * where Conditions is an expression. If Condition evaluates to
  41. * false, then the exception of the type ExceptionType is thrown
  42. * with Message.
  43. *
  44. * When an exception is caught, the method ReportException can be
  45. * used to write the information about the exception to the
  46. * Journalist, using the level J_ERROR and the category J_MAIN.
  47. *
  48. */
  49. class IpoptException
  50. {
  51. public:
  52. /**@name Constructors/Destructors */
  53. //@{
  54. /** Constructor */
  55. IpoptException(std::string msg, std::string file_name, Index line_number, std::string type="IpoptException")
  56. :
  57. msg_(msg),
  58. file_name_(file_name),
  59. line_number_(line_number),
  60. type_(type)
  61. {}
  62. /** Copy Constructor */
  63. IpoptException(const IpoptException& copy)
  64. :
  65. msg_(copy.msg_),
  66. file_name_(copy.file_name_),
  67. line_number_(copy.line_number_),
  68. type_(copy.type_)
  69. {}
  70. /** Default destructor */
  71. virtual ~IpoptException()
  72. {}
  73. //@}
  74. /** Method to report the exception to a journalist */
  75. void ReportException(const Journalist& jnlst,
  76. EJournalLevel level = J_ERROR) const
  77. {
  78. jnlst.Printf(level, J_MAIN,
  79. "Exception of type: %s in file \"%s\" at line %d:\n Exception message: %s\n",
  80. type_.c_str(), file_name_.c_str(), line_number_, msg_.c_str());
  81. }
  82. const std::string& Message() const
  83. {
  84. return msg_;
  85. }
  86. private:
  87. /**@name Default Compiler Generated Methods
  88. * (Hidden to avoid implicit creation/calling).
  89. * These methods are not implemented and
  90. * we do not want the compiler to implement
  91. * them for us, so we declare them private
  92. * and do not define them. This ensures that
  93. * they will not be implicitly created/called. */
  94. //@{
  95. /** Default Constructor */
  96. IpoptException();
  97. /** Overloaded Equals Operator */
  98. void operator=(const IpoptException&);
  99. //@}
  100. std::string msg_;
  101. std::string file_name_;
  102. Index line_number_;
  103. std::string type_;
  104. };
  105. } // namespace Ipopt
  106. #define THROW_EXCEPTION(__except_type, __msg) \
  107. throw __except_type( (__msg), (__FILE__), (__LINE__) );
  108. #define ASSERT_EXCEPTION(__condition, __except_type, __msg) \
  109. if (! (__condition) ) { \
  110. std::string newmsg = #__condition; \
  111. newmsg += " evaluated false: "; \
  112. newmsg += __msg; \
  113. throw __except_type( (newmsg), (__FILE__), (__LINE__) ); \
  114. }
  115. #define DECLARE_STD_EXCEPTION(__except_type) \
  116. class __except_type : public Ipopt::IpoptException \
  117. { \
  118. public: \
  119. __except_type(std::string msg, std::string fname, Ipopt::Index line) \
  120. : Ipopt::IpoptException(msg,fname,line, #__except_type) {} \
  121. __except_type(const __except_type& copy) \
  122. : Ipopt::IpoptException(copy) {} \
  123. private: \
  124. __except_type(); \
  125. void operator=(const __except_type&); \
  126. }
  127. #endif