GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GException.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GException.cpp - exception handlers *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2006-2017 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GException.cpp
23  * @brief Exception handler interface implementation
24  * @author J. Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #include "GException.hpp"
29 #include "GTools.hpp"
30 
31 
32 /***********************************************************************//**
33  * @brief Exception message.
34  ***************************************************************************/
35 const char* GExceptionHandler::what() const throw()
36 {
37  // Set error message
38  std::string message = "*** ERROR in " + m_origin + ": " + m_message;
39 
40  // Return message as C character array
41  return (gammalib::tochar(message));
42 }
43 
44 
45 /***********************************************************************//**
46  * @brief Invalid value
47  *
48  * @param[in] origin Name of method that has thrown the exception.
49  * @param[in] message Error message.
50  ***************************************************************************/
51 GException::invalid_value::invalid_value(const std::string& origin,
52  const std::string& message)
53 {
54  // Set origin
55  m_origin = origin;
56 
57  // Set message string
58  m_message = "Invalid value.";
59  if (message.length() > 0) {
60  m_message += (" " + message);
61  }
62 
63  // Return
64  return;
65 }
66 
67 
68 /***********************************************************************//**
69  * @brief Invalid argument
70  *
71  * @param[in] origin Name of method that has thrown the exception.
72  * @param[in] message Error message.
73  ***************************************************************************/
75  const std::string& message)
76 {
77  // Set origin
78  m_origin = origin;
79 
80  // Set message string
81  m_message = "Invalid argument.";
82  if (message.length() > 0) {
83  m_message += (" " + message);
84  }
85 
86  // Return
87  return;
88 }
89 
90 
91 /***********************************************************************//**
92  * @brief Invalid return value
93  *
94  * @param[in] origin Name of method that has thrown the exception.
95  * @param[in] message Error message.
96  ***************************************************************************/
98  const std::string& message)
99 {
100  // Set origin
101  m_origin = origin;
102 
103  // Set message string
104  m_message = "Invalid return value.";
105  if (message.length() > 0) {
106  m_message += (" " + message);
107  }
108 
109  // Return
110  return;
111 }
112 
113 
114 /***********************************************************************//**
115  * @brief Index is out of range [0,elements-1]
116  *
117  * @param[in] origin Method throwing the exception.
118  * @param[in] what Describes what is out of range.
119  * @param[in] index Index.
120  * @param[in] elements Number of elements.
121  * @param[in] message Optional error message.
122  *
123  * The @p what string specifies the index type that is out of range. For
124  * example what="Vector index" will lead to "Vector index <index> is ...".
125  * The first letter in @p what is expected to be a capital letter.
126  ***************************************************************************/
127 GException::out_of_range::out_of_range(const std::string& origin,
128  const std::string& what,
129  const int& index,
130  const int& elements,
131  const std::string& message)
132 {
133  // Set origin
134  m_origin = origin;
135 
136  // Set message
137  if (elements > 0) {
138  m_message = gammalib::strip_whitespace(what) + " " +
139  gammalib::str(index) + " is outside the"
140  " valid range [0," + gammalib::str(elements-1) + "].";
141  }
142  else {
143  m_message = "Invalid access to empty object with " +
145  " " + gammalib::str(index) + ".";
146  }
147  if (message.length() > 0) {
148  m_message += (" " + message);
149  }
150 
151  // Return
152  return;
153 }
154 
155 
156 /***********************************************************************//**
157  * @brief Runtime error
158  *
159  * @param[in] origin Name of method that has thrown the exception.
160  * @param[in] message Error message.
161  ***************************************************************************/
163  const std::string& message)
164 {
165  // Set origin
166  m_origin = origin;
167 
168  // Set message string
169  m_message = "Runtime error.";
170  if (message.length() > 0) {
171  m_message += (" " + message);
172  }
173 
174  // Return
175  return;
176 }
177 
178 
179 /***********************************************************************//**
180  * @brief File error
181  *
182  * @param[in] origin Name of method that has thrown the exception.
183  * @param[in] message Error message.
184  ***************************************************************************/
185 GException::file_error::file_error(const std::string& origin,
186  const std::string& message)
187 {
188  // Set origin
189  m_origin = origin;
190 
191  // Set message string
192  m_message = "File error.";
193  if (message.length() > 0) {
194  m_message += (" " + message);
195  }
196 
197  // Return
198  return;
199 }
200 
201 
202 /***********************************************************************//**
203  * @brief Feature not implement
204  *
205  * @param[in] origin Name of method that has thrown the exception.
206  * @param[in] message Optional message.
207  *
208  * This exception signals features that are not yet implemented. It may be
209  * thrown by modules that are still under development.
210  ***************************************************************************/
212  const std::string& message)
213 {
214  // Set origin
215  m_origin = origin;
216 
217  // Set message string
218  if (message.length() > 0) {
219  m_message = message;
220  }
221  else {
222  m_message = "Feature not implemented.";
223  }
224  m_message += " In case that you need this feature for your application"
225  " please submit a feature request on"
226  " https://cta-redmine.irap.omp.eu/projects/gammalib/,"
227  " join this error message and provide a detailed"
228  " description of your needs.";
229 
230  // Return
231  return;
232 }
233 
234 
235 /***********************************************************************//**
236  * @brief Invalid argument
237  *
238  * @param[in] origin Name of method that has thrown the exception.
239  * @param[in] argument Argument name.
240  * @param[in] message Optional message.
241  *
242  * This exception signals that a specified argument was not valid.
243  ***************************************************************************/
245  const std::string& argument,
246  const std::string& message)
247 {
248  // Set origin
249  m_origin = origin;
250 
251  // Set message string
252  m_message = "Invalid argument \""+argument+"\" value.";
253 
254  // Add optional error message
255  if (message.length() > 0) {
256  m_message += " " + message;
257  }
258 
259  // Return
260  return;
261 }
262 
263 
264 /***********************************************************************//**
265  * @brief Invalid type conversion
266  *
267  * @param[in] origin Name of method that has thrown the exception.
268  * @param[in] message Optional message.
269  ***************************************************************************/
270 GException::bad_type::bad_type(std::string origin, std::string message)
271 {
272  // Set origin
273  m_origin = origin;
274 
275  // Set message string
276  m_message = "Invalid type conversion.";
277  if (message.length() > 0) {
278  m_message += " " + message;
279  }
280 
281  // Return
282  return;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Environment variable not found
288  *
289  * @param[in] origin Name of method that has thrown the exception.
290  * @param[in] envname Name of environment variable.
291  * @param[in] message Optional message.
292  *
293  * This exception signals that are required environment variable was not
294  * found.
295  ***************************************************************************/
297  std::string envname,
298  std::string message)
299 {
300  // Set origin
301  m_origin = origin;
302 
303  // Set message string
304  m_message = "Environment variable \""+envname+"\" not found.";
305  if (message.length() > 0) {
306  m_message += " " + message;
307  }
308 
309  // Return
310  return;
311 }
312 
313 
314 /***********************************************************************//**
315  * @brief Memory allocation exception.
316  *
317  * @param[in] origin Name of method that has thrown the exception.
318  * @param[in] num Requested amount of memory.
319  ***************************************************************************/
320 GException::mem_alloc::mem_alloc(std::string origin, unsigned num)
321 {
322  // Set origin
323  m_origin = origin;
324 
325  // Set message string
326  m_message = "Memory allocation error (" + gammalib::str((int)num) + " elements)";
327 
328  // Return
329  return;
330 }
331 
332 
333 /***********************************************************************//**
334  * @brief Not enough nodes in node array
335  *
336  * @param[in] origin Name of method that has thrown the exception.
337  * @param[in] num Number of nodes in node array.
338  ***************************************************************************/
340 {
341  // Set origin
342  m_origin = origin;
343 
344  // Set message string
345  m_message = "Not enough nodes in node array (" + gammalib::str(num) + " nodes).";
346 
347  // Return
348  return;
349 }
350 
351 
352 /***********************************************************************//**
353  * @brief File not found
354  *
355  * @param[in] origin Name of method that has thrown the exception.
356  * @param[in] filename Filename.
357  * @param[in] message Optional message.
358  ***************************************************************************/
360  std::string filename,
361  std::string message)
362 {
363  // Set origin
364  m_origin = origin;
365 
366  // Set message string
367  m_message = "File \"" + filename +"\" not found.";
368  if (message.length() > 0) {
369  m_message += " " + message;
370  }
371 
372  // Return
373  return;
374 }
375 
376 
377 /***********************************************************************//**
378  * @brief File open error
379  *
380  * @param[in] origin Name of method that has thrown the exception.
381  * @param[in] filename Filename.
382  * @param[in] message Optional message.
383  ***************************************************************************/
385  std::string filename,
386  std::string message)
387 {
388  // Set origin
389  m_origin = origin;
390 
391  // Set message string
392  m_message = "Unable to open file \"" + filename +"\"";
393  if (message.length() > 0) {
394  m_message += " " + message;
395  }
396 
397  // Return
398  return;
399 }
400 
401 
402 /***********************************************************************//**
403  * @brief Directory not found
404  *
405  * @param[in] origin Name of method that has thrown the exception.
406  * @param[in] dirname Directory name.
407  * @param[in] message Optional message.
408  ***************************************************************************/
410  std::string dirname,
411  std::string message)
412 {
413  // Set origin
414  m_origin = origin;
415 
416  // Set message string
417  m_message = "Directory \"" + dirname +"\" not found.";
418  if (message.length() > 0) {
419  m_message += " " + message;
420  }
421 
422  // Return
423  return;
424 }
425 
426 
427 /***********************************************************************//**
428  * @brief Directory not accessible
429  *
430  * @param[in] origin Name of method that has thrown the exception.
431  * @param[in] dirname Directory name.
432  * @param[in] message Optional message.
433  ***************************************************************************/
435  std::string dirname,
436  std::string message)
437 {
438  // Set origin
439  m_origin = origin;
440 
441  // Set message string
442  m_message = "Directory \"" + dirname +"\" not accessible.";
443  if (message.length() > 0) {
444  m_message += " " + message;
445  }
446 
447  // Return
448  return;
449 }
450 
451 
452 /***********************************************************************//**
453  * @brief Bad CSV columns encountered
454  *
455  * @param[in] origin Name of method that has thrown the exception.
456  * @param[in] filename Filename.
457  * @param[in] row CSV file row.
458  * @param[in] cols Expected number of columns.
459  * @param[in] elements Found number of columns.
460  * @param[in] message Optional message.
461  ***************************************************************************/
463  std::string filename,
464  int row,
465  int cols,
466  int elements,
467  std::string message)
468 {
469  m_origin = origin;
470  m_message = "Inconsistent number of columns found in row "+gammalib::str(row)+
471  " of file \""+filename+"\" ("+gammalib::str(cols)+" expected, "+
472  gammalib::str(elements)+" encountered). "+message;
473  return;
474 }
directory_not_accessible(std::string origin, std::string dirname, std::string message="")
Directory not accessible.
Definition: GException.cpp:434
directory_not_found(std::string origin, std::string dirname, std::string message="")
Directory not found.
Definition: GException.cpp:409
file_error(const std::string &origin, const std::string &message="")
File error.
Definition: GException.cpp:185
invalid_value(const std::string &origin, const std::string &message)
Invalid value.
Definition: GException.cpp:51
not_enough_nodes(std::string origin, int num)
Not enough nodes in node array.
Definition: GException.cpp:339
Gammalib tools definition.
env_not_found(std::string origin, std::string envname, std::string message="")
Environment variable not found.
Definition: GException.cpp:296
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition: GTools.cpp:73
bad_type(std::string origin, std::string message="")
Invalid type conversion.
Definition: GException.cpp:270
char * tochar(const std::string &arg)
Convert string to C string.
Definition: GTools.cpp:646
runtime_error(const std::string &origin, const std::string &message="")
Runtime error.
Definition: GException.cpp:162
std::string m_message
Definition: GException.hpp:51
mem_alloc(std::string origin, unsigned num)
Memory allocation exception.
Definition: GException.cpp:320
virtual const char * what() const
Exception message.
Definition: GException.cpp:35
invalid_return_value(const std::string &origin, const std::string &message)
Invalid return value.
Definition: GException.cpp:97
file_open_error(std::string origin, std::string filename, std::string message="")
File open error.
Definition: GException.cpp:384
file_not_found(std::string origin, std::string filename, std::string message="")
File not found.
Definition: GException.cpp:359
Exception handler interface definition.
std::string tolower(const std::string &s)
Convert string to lower case.
Definition: GTools.cpp:834
csv_bad_columns(std::string origin, std::string filename, int rows, int cols, int elements, std::string message="")
Bad CSV columns encountered.
Definition: GException.cpp:462
std::string m_origin
Definition: GException.hpp:50
invalid_argument(const std::string &origin, const std::string &message)
Invalid argument.
Definition: GException.cpp:74
feature_not_implemented(const std::string &origin, const std::string &message="")
Feature not implement.
Definition: GException.cpp:211
out_of_range(std::string origin, int inx, int min, int max)
Index is out of range.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413