HermesCommon  3.0
exceptions.cpp
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http:// hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 #include "exceptions.h"
21 #include <string>
22 #include "api.h"
23 #include "callstack.h"
24 #include "util/memory_handling.h"
25 #include "mixins.h"
26 
27 namespace Hermes
28 {
29  namespace Exceptions
30  {
31  Exception::Exception() : std::exception()
32  {
33  }
34 
35  Exception::Exception(const char * msg, ...) : std::exception()
36  {
37  char text[10000];
38 
39  // print the message
40  va_list arglist;
41  va_start(arglist, msg);
42  vsprintf(text, msg, arglist);
43  va_end(arglist);
44 
45  this->message << text;
46  }
47 
49  {
50  this->message << e.message.str();
51  }
52 
53  void Exception::print_msg() const
54  {
55  std::cout << "Exception: " << message.str() << std::endl;
56  }
57 
58  const char * Exception::what() const throw()
59  {
60  sprintf(const_cast<char*>(this->message_char), "%s", this->message.str().c_str());
61  return const_cast<const char*>(this->message_char);
62  }
63 
64  std::string Exception::info() const
65  {
66  return this->message.str();
67  }
68 
69  IOException::IOException(ReadWrite readWrite, const char* filename_) : Exception(), readWrite(readWrite)
70  {
71  std::stringstream ss;
72  ss << filename;
73  this->filename = ss.str();
74  }
75 
76  IOException::IOException(ReadWrite readWrite, std::string filename_) : Exception(), readWrite(readWrite), filename(filename_)
77  {
78  }
79 
80  IOException::IOException(const IOException & e)
81  {
82  this->readWrite = e.readWrite;
83  this->filename = e.filename;
84  }
85 
87  {
88  this->message << "A pointer is invalid (nullptr)";
89  }
90 
91  NullException::NullException(unsigned int param_idx) : Exception()
92  {
93  this->param_idx = param_idx;
94  this->item_idx = -1;
95  this->message.clear();
96  this->message << "Parameter number " << param_idx << " is nullptr";
97  }
98 
99  NullException::NullException(unsigned int param_idx, unsigned int item_idx) : Exception()
100  {
101  this->param_idx = param_idx;
102  this->item_idx = item_idx;
103  this->message.clear();
104  this->message << "Element number " << item_idx << " of parameter number " << param_idx << " is nullptr";
105  }
106 
107  unsigned int NullException::get_param_idx() const
108  {
109  return param_idx;
110  }
111 
112  unsigned int NullException::get_item_idx() const
113  {
114  return item_idx;
115  }
116 
118  {
119  this->message.clear();
120  this->message << e.message.str();
121  param_idx = e.get_param_idx();
122  item_idx = e.get_item_idx();
123  }
124 
126  {
127  this->message.clear();
128  this->message << "Two instances do not have the same length and they should";
129  }
130 
131  LengthException::LengthException(unsigned int wrong, unsigned int right) : Exception()
132  {
133  this->wrong = wrong;
134  this->right = right;
135  this->message.clear();
136  this->message << "An instance has length " << wrong << " and should have " << right;
137  }
138 
139  LengthException::LengthException(unsigned int param_idx, unsigned int wrong, unsigned int right) : Exception()
140  {
141  fst_param_idx = param_idx;
142  this->wrong = wrong;
143  this->right = right;
144  this->snd_param_idx = -1;
145  this->message.clear();
146  this->message << "Parameter number " << fst_param_idx << " have length " << wrong << " and should have " << right;
147  }
148 
149  LengthException::LengthException(unsigned int fst_param_idx, unsigned int snd_param_idx, unsigned int first, unsigned int second) : Exception()
150  {
151  this->fst_param_idx = fst_param_idx;
152  this->snd_param_idx = snd_param_idx;
153  this->wrong = first;
154  this->right = second;
155  this->message.clear();
156  this->message << "Parameter number " << fst_param_idx << " have length " << wrong << " and parameter number " << snd_param_idx << " have length " << right << " The lengths should be same.";
157  }
158 
160  {
161  return fst_param_idx;
162  }
163 
165  {
166  return snd_param_idx;
167  }
168 
170  {
171  return wrong;
172  }
173 
175  {
176  return right;
177  }
178 
180  {
181  this->message << e.message.str();
182  this->fst_param_idx = e.get_first_param_idx();
183  this->snd_param_idx = e.get_second_param_idx();
184  this->wrong = e.get_first_length();
185  this->right = e.get_expected_length();
186  }
187 
189  {
190  this->message << "Linear solver failed.";
191  }
192 
194  {
195  char text[10000];
196 
197  // print the message
198  va_list arglist;
199  va_start(arglist, reason);
200  vsprintf(text, reason, arglist);
201  va_end(arglist);
202 
203  this->message << "Linear solver failed because: " << text;
204  }
205 
207  {
208  this->message << e.message.str();
209  }
210 
211  ValueException::ValueException(const char * name, double value, double allowed) : Exception()
212  {
213  if (value > allowed)
214  this->message << "Variable " << name << " is " << value << " but maximum allowed value is " << allowed;
215  else
216  this->message << "Variable " << name << " is " << value << " but minimum allowed value is " << allowed;
217  this->value = value;
218  this->allowed = allowed;
219  }
220 
221  ValueException::ValueException(const char * name, double value, double min, double max) : Exception()
222  {
223  this->message << "Variable " << name << " is " << value << " allowed range is " << min << " -- " << max;
224  this->value = value;
225  if (value > min)
226  this->allowed = max;
227  else
228  this->allowed = min;
229  }
230 
231  ValueException::ValueException(const char * name, std::string passed) : Exception()
232  {
233  this->message << "Variable " << name << " does not support value " << passed;
234  }
235 
237  {
238  return value;
239  }
240 
242  {
243  return allowed;
244  }
245 
247  {
248  this->message << e.message.str();
249  this->value = e.get_value();
250  this->allowed = e.get_allowed();
251  }
252 
254  {
255  char text[10000];
256 
257  // print the message
258  va_list arglist;
259  va_start(arglist, name);
260  vsprintf(text, name, arglist);
261  va_end(arglist);
262 
263  this->message << "Method not overriden: " << text;
264  }
265 
267  {
268  this->message << e.message.str();
269  }
270 
272  {
273  char text[10000];
274 
275  // print the message
276  va_list arglist;
277  va_start(arglist, name);
278  vsprintf(text, name, arglist);
279  va_end(arglist);
280 
281  this->message << "Sorry, method not implemented so far: " << text;
282  }
283 
285  {
286  this->message << e.message.str();
287  }
288 
290  {
291  char text[10000];
292 
293  // print the message
294  va_list arglist;
295  va_start(arglist, reason);
296  vsprintf(text, reason, arglist);
297  va_end(arglist);
298 
299  this->message << "Mesh loading failed: " << (std::string)text;
300  }
301 
303  {
304  this->message << e.message.str();
305  }
306 
308  {
309  char text[10000];
310 
311  // print the message
312  va_list arglist;
313  va_start(arglist, reason);
314  vsprintf(text, reason, arglist);
315  va_end(arglist);
316 
317  this->message << "Space loading failed: " << text;
318  }
319 
321  {
322  this->message << e.message.str();
323  }
324 
326  {
327  char text[10000];
328 
329  // print the message
330  va_list arglist;
331  va_start(arglist, reason);
332  vsprintf(text, reason, arglist);
333  va_end(arglist);
334 
335  this->message << "Solution saving failed: " << text;
336  }
337 
339  {
340  this->message << e.message.str();
341  }
342 
344  {
345  char text[10000];
346 
347  // print the message
348  va_list arglist;
349  va_start(arglist, reason);
350  vsprintf(text, reason, arglist);
351  va_end(arglist);
352 
353  this->message << "Solution loading failed: " << text;
354  }
355 
357  {
358  this->message << e.message.str();
359  }
360  }
361 }
IOException(ReadWrite readWrite, const char *filename)
Definition: exceptions.cpp:69
General namespace for the Hermes library.
LengthException()
Two parameters do not have equal length.
Definition: exceptions.cpp:125
Exception interface Basically a std::exception, but with a constructor with string and with print_msg...
Definition: exceptions.h:49
Main Hermes API.
Parameter length parameter exception. Internal. Exception occurs when some parameter has wrong length...
Definition: exceptions.h:126
char message_char[10000]
To be compatible with what().
Definition: exceptions.h:70
File containing common definitions, and basic global enums etc. for HermesCommon. ...
Definition: compat.h:85
unsigned int get_second_param_idx() const
Definition: exceptions.cpp:164
unsigned int get_first_length() const
Definition: exceptions.cpp:169
NullException()
Constructor - simple.
Definition: exceptions.cpp:86
Mesh failed to load. Thrown by Hermes2D::MeshReaderH2DXML, MeshReaderH2D.
Definition: exceptions.h:224
unsigned int get_param_idx() const
Definition: exceptions.cpp:107
virtual const char * what() const
get pointer to error message
Definition: exceptions.cpp:58
unsigned int get_first_param_idx() const
Definition: exceptions.cpp:159
double get_allowed() const
return allowed value of variable.
Definition: exceptions.cpp:241
File containing functionality for investigating call stack.
File containing definition of exceptions classes.
unsigned int get_item_idx() const
Definition: exceptions.cpp:112
void print_msg() const
print error message to stderr
Definition: exceptions.cpp:53
LinearMatrixSolverException()
Linear solver failed from unknown reason.
Definition: exceptions.cpp:188
unsigned int get_expected_length() const
Definition: exceptions.cpp:174
Exception()
Init exception with default message.
Definition: exceptions.cpp:31
Mix-in classes for one functionality, for various classes to be derived from.
Null parameter exception. Internal. Exception occurs when some parameter is Null or empty and it shou...
Definition: exceptions.h:101
Method is not overriden and should be.
Definition: exceptions.h:201
Numeric value is out of allowed range.
Definition: exceptions.h:174
ValueException(const char *name, double value, double allowed)
Definition: exceptions.cpp:211
Method is not overriden and should be.
Definition: exceptions.h:212