GammaLib 2.2.0.dev
Loading...
Searching...
No Matches
GException.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GException.cpp - Exception handler *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2006-2026 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 Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#include "GException.hpp"
29#include "GTools.hpp"
30#include "GFitsCfitsio.hpp"
31#include "GEnergy.hpp"
32
33
34/***********************************************************************//**
35 * @brief Void constructor
36 ***************************************************************************/
38{
39 // Initialise exception message memory pointer
40 m_str = NULL;
41
42 // Return
43 return;
44}
45
46
47/***********************************************************************//**
48 * @brief Destructor
49 ***************************************************************************/
51{
52 // Free exception message memory
53 if (m_str != NULL) {
54 delete [] m_str;
55 }
56
57 // Return
58 return;
59}
60
61/***********************************************************************//**
62 * @brief Exception message
63 *
64 * @return Pointer to exception message C string.
65 ***************************************************************************/
66const char* GExceptionHandler::what() const throw()
67{
68 // Set exception message
69 std::string message = "*** ERROR in " + m_origin + ": " + m_message;
70
71 // Get length of exception message
72 int n = message.length();
73
74 // Allocate C string to hold exception message
75 m_str = new char[n+1];
76
77 // Copy characters
78 for (std::size_t i = 0; i < n; ++i) {
79 m_str[i] = message[i];
80 }
81
82 // Set C string termination character
83 m_str[n] = '\0';
84
85 // Return pointer to C string
86 return (m_str);
87}
88
89
90/***********************************************************************//**
91 * @brief Invalid value
92 *
93 * @param[in] origin Name of method that has thrown the exception.
94 * @param[in] message Error message.
95 ***************************************************************************/
97 const std::string& message)
98{
99 // Set origin
100 m_origin = origin;
101
102 // Set message string
103 m_message = "Invalid value.";
104 if (message.length() > 0) {
105 m_message += (" " + message);
106 }
107
108 // Return
109 return;
110}
111
112
113/***********************************************************************//**
114 * @brief Invalid argument
115 *
116 * @param[in] origin Name of method that has thrown the exception.
117 * @param[in] message Error message.
118 ***************************************************************************/
120 const std::string& message)
121{
122 // Set origin
123 m_origin = origin;
124
125 // Set message string
126 m_message = "Invalid argument.";
127 if (message.length() > 0) {
128 m_message += (" " + message);
129 }
130
131 // Return
132 return;
133}
134
135
136/***********************************************************************//**
137 * @brief Invalid argument
138 *
139 * @param[in] origin Name of method that has thrown the exception.
140 * @param[in] argument Argument name.
141 * @param[in] message Optional message.
142 *
143 * This exception signals that a specified argument was not valid.
144 ***************************************************************************/
146 const std::string& argument,
147 const std::string& message)
148{
149 // Set origin
150 m_origin = origin;
151
152 // Set message string
153 m_message = "Invalid argument \""+argument+"\" value.";
154
155 // Add optional error message
156 if (message.length() > 0) {
157 m_message += (" " + message);
158 }
159
160 // Return
161 return;
162}
163
164
165/***********************************************************************//**
166 * @brief Invalid return value
167 *
168 * @param[in] origin Name of method that has thrown the exception.
169 * @param[in] message Error message.
170 ***************************************************************************/
172 const std::string& message)
173{
174 // Set origin
175 m_origin = origin;
176
177 // Set message string
178 m_message = "Invalid return value.";
179 if (message.length() > 0) {
180 m_message += (" " + message);
181 }
182
183 // Return
184 return;
185}
186
187
188/***********************************************************************//**
189 * @brief Index is out of range [0,elements-1]
190 *
191 * @param[in] origin Method throwing the exception.
192 * @param[in] what Describes what is out of range.
193 * @param[in] index Index.
194 * @param[in] elements Number of elements.
195 * @param[in] message Optional error message.
196 *
197 * The @p what string specifies the index type that is out of range. For
198 * example what="Vector index" will lead to "Vector index <index> is ...".
199 * The first letter in @p what is expected to be a capital letter.
200 ***************************************************************************/
202 const std::string& what,
203 const int& index,
204 const int& elements,
205 const std::string& message)
206{
207 // Set origin
208 m_origin = origin;
209
210 // Set message
211 if (elements > 0) {
213 gammalib::str(index) + " is outside the"
214 " valid range [0," + gammalib::str(elements-1) + "].";
215 }
216 else {
217 m_message = "Invalid access to empty object with " +
219 " " + gammalib::str(index) + ".";
220 }
221 if (message.length() > 0) {
222 m_message += (" " + message);
223 }
224
225 // Return
226 return;
227}
228
229
230/***********************************************************************//**
231 * @brief Runtime error
232 *
233 * @param[in] origin Name of method that has thrown the exception.
234 * @param[in] message Error message.
235 ***************************************************************************/
237 const std::string& message)
238{
239 // Set origin
240 m_origin = origin;
241
242 // Set message string
243 m_message = "Runtime error.";
244 if (message.length() > 0) {
245 m_message += (" " + message);
246 }
247
248 // Return
249 return;
250}
251
252
253/***********************************************************************//**
254 * @brief General FITS error
255 *
256 * @param[in] origin Method that throws the error.
257 * @param[in] status cfitsio status.
258 * @param[in] message Optional error message.
259 ***************************************************************************/
260GException::fits_error::fits_error(const std::string& origin,
261 const int& status,
262 const std::string& message)
263{
264 // Set origin
265 m_origin = origin;
266
267 // Set FITS message
268 char err_text[31];
269 __ffgerr(status, err_text);
270 m_message = std::string(err_text);
271 m_message += " (status=" + gammalib::str(status) + ").";
272
273 // Add optional error message
274 if (message.length() > 0) {
275 m_message += (" " + message);
276 }
277
278 // Return
279 return;
280}
281
282
283/***********************************************************************//**
284 * @brief File error
285 *
286 * @param[in] origin Name of method that has thrown the exception.
287 * @param[in] message Error message.
288 ***************************************************************************/
289GException::file_error::file_error(const std::string& origin,
290 const std::string& message)
291{
292 // Set origin
293 m_origin = origin;
294
295 // Set message string
296 m_message = "File error.";
297 if (message.length() > 0) {
298 m_message += (" " + message);
299 }
300
301 // Return
302 return;
303}
304
305
306/***********************************************************************//**
307 * @brief Feature not implement
308 *
309 * @param[in] origin Name of method that has thrown the exception.
310 * @param[in] message Optional message.
311 *
312 * This exception signals features that are not yet implemented. It may be
313 * thrown by modules that are still under development.
314 ***************************************************************************/
316 const std::string& message)
317{
318 // Set origin
319 m_origin = origin;
320
321 // Set message string
322 if (message.length() > 0) {
323 m_message = message;
324 }
325 else {
326 m_message = "Feature not implemented.";
327 }
328 m_message += " In case that you need this feature for your application"
329 " please submit a feature request on"
330 " https://cta-redmine.irap.omp.eu/projects/gammalib/,"
331 " join this error message and provide a detailed"
332 " description of your needs.";
333
334 // Return
335 return;
336}
337
338
339/***********************************************************************//**
340 * @brief Test nested try error
341 *
342 * @param[in] origin Method throwing the exception.
343 * @param[in] message Optional error message.
344 ***************************************************************************/
346 const std::string& message)
347{
348 // Set origin and message
349 m_origin = origin;
350 m_message = "Nested try error ("+message+")";
351
352 // Return
353 return;
354}
355
356/***********************************************************************//**
357 * @brief Failure test
358 *
359 * @param[in] origin Method throwing the exception.
360 * @param[in] message Optional error message.
361 *
362 * Used in unit tests to notice a fail in a try block
363 ***************************************************************************/
365 const std::string& message)
366{
367 // Set origin and message
368 m_origin = origin;
369 m_message = "Failure Test ("+message+")";
370
371 // Return
372 return;
373}
374
375/***********************************************************************//**
376 * @brief Failure test
377 *
378 * @param[in] origin Method throwing the exception.
379 * @param[in] message Optional error message.
380 *
381 * Used in unit tests to notice a fail in a try block
382 ***************************************************************************/
383GException::test_error::test_error(const std::string& origin,
384 const std::string& message)
385{
386 // Set origin and message
387 m_origin = origin;
388 m_message = "Error Test ("+message+")";
389
390 // Return
391 return;
392}
393
394
395/***********************************************************************//**
396 * @brief Checks energy interval
397 *
398 * @param[in] origin Method performing the check.
399 * @param[in] emin Minimum energy.
400 * @param[in] emax Maximum energy.
401 *
402 * @exception GException::invalid_argument
403 * Minimum energy is equal or larger than maximum energy.
404 *
405 * Checks that the minimum energy is smaller than the maximum energy.
406 ***************************************************************************/
407void gammalib::check_energy_interval(const std::string& origin,
408 const GEnergy& emin,
409 const GEnergy& emax)
410{
411 // Throw an exception if energy interval is invalid
412 if (emin == emax) {
413 std::string msg = "Minimum energy "+emin.print()+" is equal to "
414 "maximum energy "+emax.print()+". Please specify "
415 "a minimum energy that is smaller than the maximum "
416 "energy.";
417 throw GException::invalid_argument(origin, msg);
418 }
419 else if (emin > emax) {
420 std::string msg = "Minimum energy "+emin.print()+" is larger than "
421 "maximum energy "+emax.print()+". Please specify a "
422 "minimum energy that is smaller than the maximum "
423 "energy.";
424 throw GException::invalid_argument(origin, msg);
425 }
426
427 // Return
428 return;
429}
430
431
432/***********************************************************************//**
433 * @brief Checks status of GWcs::prj_x2s method
434 *
435 * @param[in] origin Method performing the check.
436 * @param[in] status Status flag.
437 * @param[in] number Number of invalid coordinates.
438 *
439 * @exception GException::invalid_argument
440 * Invalid (x,y) coordinate specified.
441 *
442 * Checks the status of the GWcs::prj_x2s method.
443 ***************************************************************************/
444void gammalib::check_prj_x2s_status(const std::string& origin,
445 const int& status,
446 const int& number)
447{
448 // Throw an exception if energy interval is invalid
449 if (status != 0) {
450 if (number == 1) {
451 std::string msg = "One invalid (x,y) coordinate specified. Please "
452 "make sure that only valid (x,y) coordinates "
453 "are specified.";
454 throw GException::invalid_argument(origin, msg);
455 }
456 else {
457 std::string msg = gammalib::str(number)+" invalid (x,y) coordinates "
458 "specified. Please make sure that only valid "
459 "(x,y) coordinates are specified.";
460 throw GException::invalid_argument(origin, msg);
461 }
462 }
463
464 // Return
465 return;
466}
467
468
469/***********************************************************************//**
470 * @brief Checks status of GWcs::prj_s2x method
471 *
472 * @param[in] origin Method performing the check.
473 * @param[in] status Status flag.
474 * @param[in] number Number of invalid coordinates.
475 *
476 * @exception GException::invalid_argument
477 * Invalid (phi,theta) coordinate specified.
478 *
479 * Checks the status of the GWcs::prj_s2x method.
480 ***************************************************************************/
481void gammalib::check_prj_s2x_status(const std::string& origin,
482 const int& status,
483 const int& number)
484{
485 // Throw an exception if energy interval is invalid
486 if (status != 0) {
487 if (number == 1) {
488 std::string msg = "One invalid (phi,theta) coordinate specified. "
489 "Please make sure that only valid (phi,theta) "
490 "coordinates are specified.";
491 throw GException::invalid_argument(origin, msg);
492 }
493 else {
494 std::string msg = gammalib::str(number)+" invalid (phi,theta) "
495 "coordinates specified. Please make sure that "
496 "only valid (phi,theta) coordinates are specified.";
497 throw GException::invalid_argument(origin, msg);
498 }
499 }
500
501 // Return
502 return;
503}
Energy value class definition.
Exception handler interface definition.
CFITSIO interface header.
#define __ffgerr(A, B)
Gammalib tools definition.
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
std::string print(const GChatter &chatter=NORMAL) const
Print energy.
Definition GEnergy.cpp:822
GExceptionHandler()
Void constructor.
virtual const char * what() const
Exception message.
virtual ~GExceptionHandler()
Destructor.
std::string m_message
Exception message.
char * m_str
Message returned by what()
std::string m_origin
Method that threw exception.
feature_not_implemented(const std::string &origin, const std::string &message="")
Feature not implement.
file_error(const std::string &origin, const std::string &message="")
File error.
fits_error(const std::string &origin, const int &status, const std::string &message="")
General FITS error.
invalid_argument(const std::string &origin, const std::string &message)
Invalid argument.
invalid_return_value(const std::string &origin, const std::string &message)
Invalid return value.
invalid_value(const std::string &origin, const std::string &message)
Invalid value.
out_of_range(const std::string &origin, const std::string &what, const int &index, const int &elements, const std::string &message="")
Index is out of range [0,elements-1].
runtime_error(const std::string &origin, const std::string &message="")
Runtime error.
test_error(const std::string &origin, const std::string &message="")
Failure test.
test_failure(const std::string &origin, const std::string &message="")
Failure test.
test_nested_try_error(const std::string &origin, const std::string &message="")
Test nested try error.
void check_energy_interval(const std::string &origin, const GEnergy &emin, const GEnergy &emax)
Checks energy interval.
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition GTools.cpp:1160
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508
std::string tolower(const std::string &s)
Convert string to lower case.
Definition GTools.cpp:948
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition GTools.cpp:99
void check_prj_s2x_status(const std::string &origin, const int &status, const int &number)
Checks status of GWcs::prj_s2x method.
void check_prj_x2s_status(const std::string &origin, const int &status, const int &number)
Checks status of GWcs::prj_x2s method.