Scippy

SCIP

Solving Constraint Integer Programs

scip_exception.hpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the examples to */
4 /* An introduction to SCIP */
5 /* */
6 /* Copyright (C) 2007 Cornelius Schwarz */
7 /* */
8 /* 2007 University of Bayreuth */
9 /* */
10 /* */
11 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 /**@file scip_exception.hpp
14  * @brief exception handling for SCIP
15  * @author Cornelius Schwarz
16  */
17 
18 #ifndef SCIP_EXCEPTION
19 #define SCIP_EXCEPTION
20 
21 #include <exception>
22 #include <string>
23 
24 #include <scip/scip.h>
25 #include <scip/misc.h>
26 
27 // unfortunately SCIP has no method to get the string of an error code, you can just print it to a file
28 // so we add such a method here, this has to be updated when SCIP Messages changes
29 // currently supporting SCIP-1.00
30 #define SCIP_MSG_MAX 100 ///< maximal number of characters in an error messages
31 
32 /**
33  * @brief translates a SCIP_RETCODE into an error string
34  *
35  * @param[in] retcode SCIP_RETCODE you want to translate
36  * @param[in] buffersize size of buffer
37  * @param[out] buffer_str buffer to character array to store translated message, this must be at least of size SCIP_MSG_MAX
38  * @return buffer_str or NULL, if retcode could not be translated
39  */
40 inline char* SCIPgetErrorString(SCIP_RETCODE retcode, char* buffer_str, int buffersize)
41 {
42  // the following was copied from SCIPprintError
43  switch(retcode)
44  {
45  case SCIP_OKAY:
46  (void) SCIPsnprintf(buffer_str, buffersize, "normal termination");
47  return buffer_str;
48  case SCIP_ERROR:
49  (void) SCIPsnprintf(buffer_str, buffersize, "unspecified error");
50  return buffer_str;
51  case SCIP_NOMEMORY:
52  (void) SCIPsnprintf(buffer_str, buffersize, "insufficient memory error");
53  return buffer_str;
54  case SCIP_READERROR:
55  (void) SCIPsnprintf(buffer_str, buffersize, "file read error");
56  return buffer_str;
57  case SCIP_WRITEERROR:
58  (void) SCIPsnprintf(buffer_str, buffersize, "file write error");
59  return buffer_str;
60  case SCIP_BRANCHERROR:
61  (void) SCIPsnprintf(buffer_str, buffersize, "branch error");
62  return buffer_str;
63  case SCIP_NOFILE:
64  (void) SCIPsnprintf(buffer_str, buffersize, "file not found error");
65  return buffer_str;
67  (void) SCIPsnprintf(buffer_str, buffersize, "cannot create file");
68  return buffer_str;
69  case SCIP_LPERROR:
70  (void) SCIPsnprintf(buffer_str, buffersize, "error in LP solver");
71  return buffer_str;
72  case SCIP_NOPROBLEM:
73  (void) SCIPsnprintf(buffer_str, buffersize, "no problem exists");
74  return buffer_str;
75  case SCIP_INVALIDCALL:
76  (void) SCIPsnprintf(buffer_str, buffersize, "method cannot be called at this time in solution process");
77  return buffer_str;
78  case SCIP_INVALIDDATA:
79  (void) SCIPsnprintf(buffer_str, buffersize, "method cannot be called with this type of data");
80  return buffer_str;
81  case SCIP_INVALIDRESULT:
82  (void) SCIPsnprintf(buffer_str, buffersize, "method returned an invalid result code");
83  return buffer_str;
85  (void) SCIPsnprintf(buffer_str, buffersize, "a required plugin was not found");
86  return buffer_str;
88  (void) SCIPsnprintf(buffer_str, buffersize, "the parameter with the given name was not found");
89  return buffer_str;
91  (void) SCIPsnprintf(buffer_str, buffersize, "the parameter is not of the expected type");
92  return buffer_str;
94  (void) SCIPsnprintf(buffer_str, buffersize, "the value is invalid for the given parameter");
95  return buffer_str;
97  (void) SCIPsnprintf(buffer_str, buffersize, "the given key is already existing in table");
98  return buffer_str;
99  case SCIP_MAXDEPTHLEVEL:
100  (void) SCIPsnprintf(buffer_str, buffersize, "maximal branching depth level exceeded");
101  return buffer_str;
102  case SCIP_NOTIMPLEMENTED:
103  (void) SCIPsnprintf(buffer_str, buffersize, "function not implemented");
104  return buffer_str;
105  }
106  return NULL;
107 }
108 
109 
110 /** @brief exception handling class for SCIP
111  *
112  * this class enables you to handle the return code of SCIP functions in a C++ way
113  */
114 class SCIPException : public std::exception
115 {
116 private:
117  char _msg[SCIP_MSG_MAX]; ///< error message
118  SCIP_RETCODE _retcode; ///< SCIP error code
119 
120 public:
121 
122  /** @brief constructs a SCIPEexception from an error code
123  *
124  * this constructs a new SCIPException from given error code
125  * @param[in] retcode SCIP error code
126  */
127  SCIPException(SCIP_RETCODE retcode) : _retcode(retcode)
128  {
129  if( SCIPgetErrorString(retcode, _msg, SCIP_MSG_MAX) == NULL )
130  (void) SCIPsnprintf(_msg, SCIP_MSG_MAX, "unknown SCIP retcode %d", retcode);
131  }
132 
133 
134  /** @brief returns the error message
135  *
136  * @return error message
137  *
138  * this overrides the corresponding method of std::exception in order to allow you to catch all of your exceptions as std::exception
139  */
140  const char* what(void) const throw() {return _msg;}
141 
142 
143  /** @brief get method for @p _retcode
144  *
145  * @return stored SCIP_RETCODE
146  */
147  SCIP_RETCODE getRetcode(void) const {return _retcode;}
148 
149  /** destructor */
150  ~SCIPException(void) {}
151 }; /*lint !e1712*/
152 
153 
154 /** @brief macro to call scip function with exception handling
155  *
156  * this macro calls a SCIP function and throws an instance of SCIPException if anything went wrong
157  *
158  */
159 #define SCIP_CALL_EXC(x) \
160  { \
161  SCIP_RETCODE retcode; \
162  if( (retcode = (x)) != SCIP_OKAY) \
163  { \
164  throw SCIPException(retcode); \
165  } \
166  }
167 
168 
169 #endif
#define SCIP_MSG_MAX
maximal number of characters in an error messages
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10755
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
char * SCIPgetErrorString(SCIP_RETCODE retcode, char *buffer_str, int buffersize)
translates a SCIP_RETCODE into an error string
exception handling class for SCIP
internal miscellaneous methods
#define NULL
Definition: lpi_spx1.cpp:155
SCIP_RETCODE getRetcode(void) const
get method for _retcode
const char * what(void) const
returns the error message
SCIPException(SCIP_RETCODE retcode)
constructs a SCIPEexception from an error code
SCIP callable library.