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  }
103  return NULL;
104 }
105 
106 
107 /** @brief exception handling class for SCIP
108  *
109  * this class enables you to handle the return code of SCIP functions in a C++ way
110  */
111 class SCIPException : public std::exception
112 {
113 private:
114  char _msg[SCIP_MSG_MAX]; ///< error message
115  SCIP_RETCODE _retcode; ///< SCIP error code
116 
117 public:
118 
119  /** @brief constructs a SCIPEexception from an error code
120  *
121  * this constructs a new SCIPException from given error code
122  * @param[in] retcode SCIP error code
123  */
124  SCIPException(SCIP_RETCODE retcode) : _retcode(retcode)
125  {
126  if( SCIPgetErrorString(retcode, _msg, SCIP_MSG_MAX) == NULL )
127  (void) SCIPsnprintf(_msg, SCIP_MSG_MAX, "unknown SCIP retcode %d",retcode);
128  }
129 
130 
131  /** @brief returns the error message
132  *
133  * @return error message
134  *
135  * this overrides the corresponding method of std::exception in order to allow you to catch all of your exceptions as std::exception
136  */
137  const char * what(void)const throw() {return _msg;}
138 
139 
140  /** @brief get method for @p _retcode
141  *
142  * @return stored SCIP_RETCODE
143  */
144  SCIP_RETCODE getRetcode(void)const{return _retcode;}
145 
146  /** destructor */
147  ~SCIPException(void) throw(){}
148 }; /*lint !e1712*/
149 
150 
151 /** @brief macro to call scip function with exception handling
152  *
153  * this macro calls a SCIP function and throws an instance of SCIPException if anything went wrong
154  *
155  */
156 #define SCIP_CALL_EXC(x) \
157  { \
158  SCIP_RETCODE retcode; \
159  if( (retcode = (x)) != SCIP_OKAY) \
160  { \
161  throw SCIPException(retcode); \
162  } \
163  }
164 
165 
166 #endif
#define NULL
Definition: def.h:239
#define SCIP_MSG_MAX
maximal number of characters in an error messages
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10017
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
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
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.