HermesCommon  2.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 
25 namespace Hermes
26 {
27  namespace Exceptions
28  {
29  Exception::Exception() : std::exception(), message(new char[1000])
30  {
31  }
32 
33  Exception::Exception(const char * msg, ...) : std::exception(), message(new char[1000])
34  {
35  char text[1024];
36 
37  // print the message
38  va_list arglist;
39  va_start(arglist, msg);
40  vsprintf(text, msg, arglist);
41  va_end(arglist);
42 
43  strcpy(message, text);
44  }
45 
46  void Exception::print_msg() const
47  {
48  if(message)
49  printf("Exception: %s\n", message);
50  else
51  printf("Default exception\n");
52  if(Hermes::HermesCommonApi.get_integral_param_value(Hermes::exceptionsPrintCallstack) == 1)
53  CallStack::dump(0);
54  }
55 
56  Exception* Exception::clone()
57  {
58  return new Exception(*this);
59  }
60 
61  const char * Exception::what() const throw()
62  {
63  char* messageWithReturn = new char[strlen(message)+2];
64  strcpy(messageWithReturn, message);
65  sprintf(messageWithReturn + strlen(message), "\n");
66  return messageWithReturn;
67  }
68 
70  {
71  this->param_idx = param_idx;
72  this->item_idx = -1;
73  char * msg = new char[27];
74  sprintf(msg, "Parameter number %d is NULL", param_idx);
75  message = msg;
76  }
77 
78  NullException::NullException(int param_idx, int item_idx) : Exception()
79  {
80  this->param_idx = param_idx;
81  this->item_idx = item_idx;
82  char * msg = new char[55];
83  sprintf(msg, "Element number %d of parameter number %d is NULL", item_idx, param_idx);
84  message = msg;
85  }
86 
88  {
89  return param_idx;
90  }
91 
93  {
94  return item_idx;
95  }
96 
98  {
99  char * msg = new char[strlen(e.what())+1];
100  strcpy(msg, e.what());
101  message = msg;
102  param_idx = e.get_param_idx();
103  item_idx = e.get_item_idx();
104  }
105 
106  Exception* NullException::clone()
107  {
108  return new NullException(*this);
109  }
110 
111  LengthException::LengthException(int param_idx, int wrong, int right) : Exception()
112  {
113  fst_param_idx = param_idx;
114  this->wrong = wrong;
115  this->right = right;
116  this->snd_param_idx = -1;
117  char * msg = new char[60];
118  sprintf(msg, "Parameter number %d have length %d and should have %d", fst_param_idx, wrong, right);
119  message = msg;
120  }
121 
122  LengthException::LengthException(int fst_param_idx, int snd_param_idx, int first, int second) : Exception()
123  {
124  this->fst_param_idx = fst_param_idx;
125  this->snd_param_idx = snd_param_idx;
126  this->wrong = first;
127  this->right = second;
128  char * msg = new char[110];
129  sprintf(msg, "Parameter number %d have length %d and parameter number %d have length %d. The lengths should be same",
130  fst_param_idx, wrong, snd_param_idx, right);
131  message = msg;
132  }
133 
135  {
136  return fst_param_idx;
137  }
138 
140  {
141  return snd_param_idx;
142  }
143 
145  {
146  return wrong;
147  }
148 
150  {
151  return right;
152  }
153 
155  {
156  char * msg = new char[strlen(e.what())+1];
157  strcpy(msg, e.what());
158  message = msg;
159  this->fst_param_idx = e.get_first_param_idx();
160  this->snd_param_idx = e.get_second_param_idx();
161  this->wrong = e.get_first_length();
162  this->right = e.get_expected_length();
163  }
164 
165  Exception* LengthException::clone()
166  {
167  return new LengthException(*this);
168  }
169 
171  {
172  char * msg = new char[22];
173  sprintf(msg, "Linear solver failed.");
174  message = msg;
175  }
176 
178  {
179  char * msg = new char[34 + strlen(reason)];
180  sprintf(msg, "Linear solver failed because:\"%s\"", reason);
181  message = msg;
182  }
183 
185  {
186  char * msg = new char[strlen(e.what())+1];
187  strcpy(msg, e.what());
188  message = msg;
189  }
190 
191  Exception* LinearMatrixSolverException::clone()
192  {
193  return new LinearMatrixSolverException(*this);
194  }
195 
196  ValueException::ValueException(const char * name, double value, double allowed) : Exception()
197  {
198  char * msg = new char[55 + strlen(name)];
199  if(value>allowed)
200  sprintf(msg, "Variable %s is %f but maximum allowed value is %f", name, value, allowed);
201  else
202  sprintf(msg, "Variable %s is %f but minimum allowed value is %f", name, value, allowed);
203  message = msg;
204  this->value = value;
205  this->allowed = allowed;
206  }
207 
208  ValueException::ValueException(const char * name, double value, double min, double max) : Exception()
209  {
210  char * msg = new char[70+strlen(name)];
211  sprintf(msg, "Variable %s is %f allowed range is %f -- %f", name, value, min, max);
212  message = msg;
213  this->value = value;
214  if(value>min)
215  this->allowed = max;
216  else
217  this->allowed = min;
218  }
219 
220  ValueException::ValueException(const char * name, std::string passed) : Exception()
221  {
222  char * msg = new char[70+strlen(name)];
223  sprintf(msg, "Variable %s does not support value %s.", name, passed.c_str());
224  message = msg;
225  }
226 
228  {
229  return value;
230  }
231 
233  {
234  return allowed;
235  }
236 
238  {
239  char * msg = new char[strlen(e.what())+1];
240  strcpy(msg, e.what());
241  message = msg;
242  this->value = e.get_value();
243  this->allowed = e.get_allowed();
244  }
245 
246  Exception* ValueException::clone()
247  {
248  return new ValueException(*this);
249  }
250 
252  {
253  char* text = new char[1024];
254  sprintf(text, "Method not overriden: ");
255 
256  // print the message
257  va_list arglist;
258  va_start(arglist, name);
259  vsprintf(text = text + strlen("Method not overriden: "), name, arglist);
260  va_end(arglist);
261  message = text - strlen("Method not overriden: ");
262  }
263 
265  {
266  char * msg = new char[strlen(e.what())+1];
267  strcpy(msg, e.what());
268  message = msg;
269  }
270 
271  Exception* MethodNotOverridenException::clone()
272  {
273  return new MethodNotOverridenException(*this);
274  }
275 
277  {
278  char * text = new char[strlen(reason)+1];
279 
280  // print the message
281  va_list arglist;
282  va_start(arglist, reason);
283  vsprintf(text, reason, arglist);
284  va_end(arglist);
285 
286  message = text;
287  }
288 
290  {
291  char * msg = new char[strlen(e.what())+1];
292  strcpy(msg, e.what());
293  message = msg;
294  }
295 
296  Exception* MeshLoadFailureException::clone()
297  {
298  return new MeshLoadFailureException(*this);
299  }
300 
302  {
303  char * text = new char[strlen(reason)+1];
304 
305  // print the message
306  va_list arglist;
307  va_start(arglist, reason);
308  vsprintf(text, reason, arglist);
309  va_end(arglist);
310 
311  message = text;
312  }
313 
315  {
316  char * msg = new char[strlen(e.what())+1];
317  strcpy(msg, e.what());
318  message = msg;
319  }
320 
321  Exception* SpaceLoadFailureException::clone()
322  {
323  return new SpaceLoadFailureException(*this);
324  }
325 
327  {
328  char * text = new char[strlen(reason)+1];
329 
330  // print the message
331  va_list arglist;
332  va_start(arglist, reason);
333  vsprintf(text, reason, arglist);
334  va_end(arglist);
335 
336  message = text;
337  }
338 
340  {
341  char * msg = new char[strlen(e.what())+1];
342  strcpy(msg, e.what());
343  message = msg;
344  }
345 
346  Exception* SolutionSaveFailureException::clone()
347  {
348  return new SolutionSaveFailureException(*this);
349  }
350 
352  {
353  char * text = new char[strlen(reason)+1];
354 
355  // print the message
356  va_list arglist;
357  va_start(arglist, reason);
358  vsprintf(text, reason, arglist);
359  va_end(arglist);
360 
361  message = text;
362  }
363 
365  {
366  char * msg = new char[strlen(e.what())+1];
367  strcpy(msg, e.what());
368  message = msg;
369  }
370 
371  Exception* SolutionLoadFailureException::clone()
372  {
373  return new SolutionLoadFailureException(*this);
374  }
375  }
376 }