File: IpException.hpp

package info (click to toggle)
coinor-ipopt 3.11.9-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,616 kB
  • sloc: cpp: 75,373; sh: 11,170; ansic: 2,798; makefile: 2,247; java: 632; fortran: 342
file content (147 lines) | stat: -rw-r--r-- 4,202 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (C) 2004, 2006 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// $Id: IpException.hpp 2023 2011-06-18 18:49:49Z stefan $
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#ifndef __IPEXCEPTION_HPP__
#define __IPEXCEPTION_HPP__

#include "IpUtils.hpp"
#include "IpJournalist.hpp"

/*  This file contains a base class for all exceptions
 *  and a set of macros to help with exceptions
 */

namespace Ipopt
{

  /** This is the base class for all exceptions.  The easiest way to
   *   use this class is by means of the following macros:
   *
   * \verbatim

     DECLARE_STD_EXCEPTION(ExceptionType);
     \endverbatim
   *
   * This macro defines a new class with the name ExceptionType,
   * inherited from the base class IpoptException.  After this,
   * exceptions of this type can be thrown using
   *
   * \verbatim

     THROW_EXCEPTION(ExceptionType, Message);
     \endverbatim
   *
   * where Message is a std::string with a message that gives an
   * indication of what caused the exception.  Exceptions can also be
   * thrown using the macro
   *
   * \verbatim

     ASSERT_EXCEPTION(Condition, ExceptionType, Message);
     \endverbatim
   *
   * where Conditions is an expression.  If Condition evaluates to
   * false, then the exception of the type ExceptionType is thrown
   * with Message.
   *
   * When an exception is caught, the method ReportException can be
   * used to write the information about the exception to the
   * Journalist, using the level J_ERROR and the category J_MAIN.
   *
   */
  class IpoptException
  {
  public:
    /**@name Constructors/Destructors */
    //@{
    /** Constructor */
    IpoptException(std::string msg, std::string file_name, Index line_number, std::string type="IpoptException")
        :
        msg_(msg),
        file_name_(file_name),
        line_number_(line_number),
        type_(type)
    {}

    /** Copy Constructor */
    IpoptException(const IpoptException& copy)
        :
        msg_(copy.msg_),
        file_name_(copy.file_name_),
        line_number_(copy.line_number_),
        type_(copy.type_)
    {}

    /** Default destructor */
    virtual ~IpoptException()
    {}
    //@}

    /** Method to report the exception to a journalist */
    void ReportException(const Journalist& jnlst,
                         EJournalLevel level = J_ERROR) const
    {
      jnlst.Printf(level, J_MAIN,
                   "Exception of type: %s in file \"%s\" at line %d:\n Exception message: %s\n",
                   type_.c_str(), file_name_.c_str(),  line_number_, msg_.c_str());
    }

    const std::string& Message() const
    {
      return msg_;
    }

  private:
    /**@name Default Compiler Generated Methods
     * (Hidden to avoid implicit creation/calling).
     * These methods are not implemented and 
     * we do not want the compiler to implement
     * them for us, so we declare them private
     * and do not define them. This ensures that
     * they will not be implicitly created/called. */
    //@{
    /** Default Constructor */
    IpoptException();

    /** Overloaded Equals Operator */
    void operator=(const IpoptException&);
    //@}

    std::string msg_;
    std::string file_name_;
    Index line_number_;
    std::string type_;
  };

} // namespace Ipopt

#define THROW_EXCEPTION(__except_type, __msg) \
  throw __except_type( (__msg), (__FILE__), (__LINE__) );

#define ASSERT_EXCEPTION(__condition, __except_type, __msg) \
  if (! (__condition) ) { \
    std::string newmsg = #__condition; \
    newmsg += " evaluated false: "; \
    newmsg += __msg; \
    throw __except_type( (newmsg), (__FILE__), (__LINE__) ); \
  }

#define DECLARE_STD_EXCEPTION(__except_type) \
    class __except_type : public Ipopt::IpoptException \
    { \
    public: \
      __except_type(std::string msg, std::string fname, Ipopt::Index line) \
 : Ipopt::IpoptException(msg,fname,line, #__except_type) {} \
      __except_type(const __except_type& copy) \
 : Ipopt::IpoptException(copy) {} \
    private: \
       __except_type(); \
       void operator=(const __except_type&); \
    }

#endif