MAiNGO
exceptions.h
Go to the documentation of this file.
1 /**********************************************************************************
2  * Copyright (c) 2019 Process Systems Engineering (AVT.SVT), RWTH Aachen University
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License 2.0 which is available at
6  * http://www.eclipse.org/legal/epl-2.0.
7  *
8  * SPDX-License-Identifier: EPL-2.0
9  *
10  * @file exceptions.h
11  *
12  * @brief File declaring the MAiNGO exception class.
13  *
14  **********************************************************************************/
15 
16 #pragma once
17 
18 #include "babNode.h"
19 
20 #include <exception>
21 #include <sstream>
22 #include <string>
23 #include <typeinfo>
24 
25 
26 namespace maingo {
27 
28 
39 class MAiNGOException: public std::exception {
40 
41  private:
42  std::string _msg{""};
44 
45  public:
51  explicit MAiNGOException(const std::string& arg):
52  MAiNGOException(arg, nullptr, nullptr)
53  {
54  }
55 
62  MAiNGOException(const std::string& arg, const babBase::BabNode& node):
63  MAiNGOException(arg, nullptr, &node)
64  {
65  }
66 
73  MAiNGOException(const std::string& arg, const std::exception& e):
74  MAiNGOException(arg, &e, nullptr)
75  {
76  }
77 
85  MAiNGOException(const std::string& arg, const std::exception& e, const babBase::BabNode& node):
86  MAiNGOException(arg, &e, &node)
87  {
88  }
89 
97  MAiNGOException(const std::string& arg, const std::exception* e, const babBase::BabNode* node)
98  {
99  std::ostringstream message;
100  if (e) {
101  if (typeid(*e).name() != typeid(*this).name()) {
102  message << " Original std::exception: " << typeid(*e).name() << ": " << std::endl
103  << " ";
104  }
105  message << e->what() << std::endl;
106  }
107  message << arg;
108  if (node) {
109  std::vector<double> lowerVarBounds(node->get_lower_bounds()), upperVarBounds(node->get_upper_bounds());
110  message << std::endl
111  << " Exception was thrown while processing node no. " << node->get_ID() << ":";
112  for (unsigned int i = 0; i < lowerVarBounds.size(); i++) {
113  message << std::endl
114  << " x(" << i << "): " << std::setprecision(16) << lowerVarBounds[i] << ":" << upperVarBounds[i];
115  }
116  }
117  _msg = message.str();
118  }
119 
120 
126  const char* what() const noexcept
127  {
128  return _msg.c_str();
129  }
130 };
131 
132 
133 #ifdef HAVE_MAiNGO_MPI
134 
140 class MAiNGOMpiException: public MAiNGOException {
141 
142  public:
147  enum TYPE {
148  MPI_ME = 1,
149  MPI_OTHER
150  };
151 
157  explicit MAiNGOMpiException(TYPE ierr):
158  MAiNGOMpiException("", nullptr, nullptr, ierr)
159  {
160  }
161 
168  MAiNGOMpiException(const std::string& arg, TYPE ierr):
169  MAiNGOMpiException(arg, nullptr, nullptr, ierr)
170  {
171  }
172 
180  MAiNGOMpiException(const std::string& arg, const babBase::BabNode& node, TYPE ierr):
181  MAiNGOMpiException(arg, nullptr, &node, ierr)
182  {
183  }
184 
192  MAiNGOMpiException(const std::string& arg, const std::exception& e, TYPE ierr):
193  MAiNGOMpiException(arg, &e, nullptr, ierr)
194  {
195  }
196 
205  MAiNGOMpiException(const std::string& arg, const std::exception& e, const babBase::BabNode& node, TYPE ierr):
206  MAiNGOMpiException(arg, &e, &node, ierr)
207  {
208  }
209 
216  MAiNGOMpiException(MAiNGOException& e, TYPE ierr):
217  MAiNGOException(e), _ierr(ierr)
218  {
219  }
220 
229  MAiNGOMpiException(const std::string& arg, const std::exception* e, const babBase::BabNode* node, TYPE ierr):
230  MAiNGOException(arg, e, node), _ierr(ierr)
231  {
232  }
233 
237  int ierr() { return _ierr; }
238 
239 
240  private:
241  TYPE _ierr;
242 };
243 #endif
244 
245 } // end namespace maingo
int get_ID() const
Function for querying the node ID.
Definition: babNode.h:100
Class representing a node in the Branch-and-Bound tree.
Definition: babNode.h:35
MAiNGOException(const std::string &arg, const std::exception &e)
Constructor used for forwarding.
Definition: exceptions.h:73
MAiNGOException(const std::string &arg)
Constructor used for forwarding.
Definition: exceptions.h:51
std::string _msg
Definition: exceptions.h:42
MAiNGOException(const std::string &arg, const babBase::BabNode &node)
Constructor used for forwarding.
Definition: exceptions.h:62
std::vector< double > get_lower_bounds() const
Function for querying the lower bounds on the optimization variables within this node.
Definition: babNode.h:90
const char * what() const noexcept
Function to return the error message.
Definition: exceptions.h:126
MAiNGOException(const std::string &arg, const std::exception &e, const babBase::BabNode &node)
Constructor used for forwarding.
Definition: exceptions.h:85
namespace holding all essentials of MAiNGO
Definition: aleModel.h:31
MAiNGOException(const std::string &arg, const std::exception *e, const babBase::BabNode *node)
Constructor used printing a MAiNGO Exception.
Definition: exceptions.h:97
std::vector< double > get_upper_bounds() const
Function for querying the upper bounds on the optimization variables within this node.
Definition: babNode.h:95
This class defines the exceptions thrown by MAiNGO.
Definition: exceptions.h:39