MAiNGO
MAiNGOException.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  **********************************************************************************/
11 
12 #pragma once
13 
14 #include "babNode.h"
15 
16 #include <exception>
17 #include <sstream>
18 #include <string>
19 #include <typeinfo>
20 
21 
22 namespace maingo {
23 
24 
35 class MAiNGOException: public std::exception {
36 
37  public:
38  MAiNGOException() = delete;
39  MAiNGOException(const MAiNGOException&) = default;
40  MAiNGOException(MAiNGOException&&) = default;
41  MAiNGOException& operator=(const MAiNGOException&) = default;
43  virtual ~MAiNGOException() = default;
44 
45  explicit MAiNGOException(const std::string& errorMessage)
46  {
47  _construct_complete_error_message(errorMessage, nullptr, nullptr);
48  }
49 
50  MAiNGOException(const std::string& errorMessage, const babBase::BabNode& nodeThatErrorOccurredIn)
51  {
52  _construct_complete_error_message(errorMessage, nullptr, &nodeThatErrorOccurredIn);
53  }
54 
55  MAiNGOException(const std::string& errorMessage, const std::exception& originalException)
56  {
57  _construct_complete_error_message(errorMessage, &originalException, nullptr);
58  }
59 
60  MAiNGOException(const std::string& errorMessage, const std::exception& originalException, const babBase::BabNode& nodeThatErrorOccurredIn)
61  {
62  _construct_complete_error_message(errorMessage, &originalException, &nodeThatErrorOccurredIn);
63  }
64 
65  const char* what() const noexcept override
66  {
67  return _errorMessage.c_str();
68  }
69 
70 
71  private:
72  std::string _errorMessage{""};
73 
74  void _construct_complete_error_message(const std::string& errorMessage, const std::exception* originalException, const babBase::BabNode* nodeThatErrorOccurredIn)
75  {
76  std::ostringstream errorMessageStream;
77 
78  _append_original_exception_info_to_message(originalException, errorMessageStream);
79  _append_current_error_message_to_message(errorMessage, errorMessageStream);
80  _append_node_info_to_message(nodeThatErrorOccurredIn, errorMessageStream);
81 
82  _errorMessage = errorMessageStream.str();
83  }
84 
85  void _append_current_error_message_to_message(const std::string& currentErrorMessage, std::ostringstream& completeErrorMessage)
86  {
87  completeErrorMessage << currentErrorMessage;
88  }
89 
90  void _append_original_exception_info_to_message(const std::exception* originalException, std::ostringstream& completeErrorMessage)
91  {
92  if (originalException) {
93  if (typeid(*originalException).name() != typeid(*this).name()) {
94  completeErrorMessage << " Original exception type: " << typeid(*originalException).name() << ": " << std::endl
95  << " ";
96  }
97  completeErrorMessage << originalException->what() << std::endl;
98  }
99  }
100 
101  void _append_node_info_to_message(const babBase::BabNode* nodeThatErrorOccurredIn, std::ostringstream& completeErrorMessage)
102  {
103  if (nodeThatErrorOccurredIn) {
104  std::vector<double> lowerVarBounds(nodeThatErrorOccurredIn->get_lower_bounds()), upperVarBounds(nodeThatErrorOccurredIn->get_upper_bounds());
105  completeErrorMessage << std::endl
106  << " Exception was thrown while processing node no. " << nodeThatErrorOccurredIn->get_ID() << ":";
107  for (size_t i = 0; i < lowerVarBounds.size(); i++) {
108  completeErrorMessage << std::endl
109  << " x(" << i << "): " << std::setprecision(16) << lowerVarBounds[i] << ":" << upperVarBounds[i];
110  }
111  }
112  }
113 };
114 
115 
116 } // end namespace maingo
std::string _errorMessage
Definition: MAiNGOException.h:72
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 &errorMessage, const std::exception &originalException)
Definition: MAiNGOException.h:55
void _append_node_info_to_message(const babBase::BabNode *nodeThatErrorOccurredIn, std::ostringstream &completeErrorMessage)
Definition: MAiNGOException.h:101
MAiNGOException(const std::string &errorMessage, const std::exception &originalException, const babBase::BabNode &nodeThatErrorOccurredIn)
Definition: MAiNGOException.h:60
std::vector< double > get_lower_bounds() const
Function for querying the lower bounds on the optimization variables within this node.
Definition: babNode.h:90
void _append_original_exception_info_to_message(const std::exception *originalException, std::ostringstream &completeErrorMessage)
Definition: MAiNGOException.h:90
const char * what() const noexcept override
Definition: MAiNGOException.h:65
MAiNGOException(const std::string &errorMessage, const babBase::BabNode &nodeThatErrorOccurredIn)
Definition: MAiNGOException.h:50
void _construct_complete_error_message(const std::string &errorMessage, const std::exception *originalException, const babBase::BabNode *nodeThatErrorOccurredIn)
Definition: MAiNGOException.h:74
namespace holding all essentials of MAiNGO
Definition: aleModel.h:25
virtual ~MAiNGOException()=default
MAiNGOException & operator=(const MAiNGOException &)=default
MAiNGOException(const std::string &errorMessage)
Definition: MAiNGOException.h:45
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: MAiNGOException.h:35
void _append_current_error_message_to_message(const std::string &currentErrorMessage, std::ostringstream &completeErrorMessage)
Definition: MAiNGOException.h:85