Commit 7b096714c6bbd793989afff6978d99e40d3f38b4

Authored by Jürgen Knödlseder
1 parent e18e1e1c

Replace specific by generic exceptions in MWL module (#35)

ChangeLog
1   -2021-05-10
  1 +2021-05-21
2 2  
3 3 * Version 2.0.0 released
4 4 ========================
... ... @@ -53,6 +53,7 @@
53 53 Add GSkyMap::flux(GRegion(s)) and GSkyMap::solidangle(GRegion(s)) methods (#1785)
54 54 Reimplement GModelSpatial::flux() method for diffuse and combined models (#1785)
55 55 Implement pre-computation cache in GModelSpatialGauss (#1300)
  56 + Replace specific by generic exceptions (#35)
56 57  
57 58  
58 59 2021-04-28
... ...
1 1 New Features and Important Changes in GammaLib 2.0.0
2 2  
3   -10 May 2021
  3 +21 May 2021
4 4  
5 5  
6 6 1. Introduction
... ... @@ -24,7 +24,8 @@ The following classes have been added:
24 24 - GCTABackground2D
25 25  
26 26 The following classes have been removed:
27   -- none
  27 +- GMWLExceptionHandler
  28 +- GMWLException
28 29  
29 30 The following classes have been renamed:
30 31 - none
... ...
README.md
1 1 GammaLib information
2 2 ====================
3   -* Version: 2.0.0.dev (10 May 2021)
  3 +* Version: 2.0.0.dev (21 May 2021)
4 4  
5 5 [![Build Status](https://cta-jenkins.irap.omp.eu/buildStatus/icon?job=gammalib-integrate-os)](https://cta-jenkins.irap.omp.eu/job/gammalib-integrate-os/)
6 6  
... ...
doc/source/admin/release_history/2.0.rst
1 1 .. _2.0:
2 2  
3   -GammaLib 2.0 release (8 May 2021)
4   -=================================
  3 +GammaLib 2.0 release (21 May 2021)
  4 +==================================
5 5  
6 6 Overview
7 7 --------
... ... @@ -120,3 +120,5 @@ Clean up
120 120 Store DRB, DRG and DRX as instances of ``GCOMDri`` instead of ``GSkyMap`` in ``GCOMObservation``
121 121 * [`2973 <https://cta-redmine.irap.omp.eu/issues/2973>`_] -
122 122 Remove COMPTEL internal response caching
  123 +* [`35 <https://cta-redmine.irap.omp.eu/issues/35>`_] -
  124 + Replace specific by generic exceptions
... ...
inst/mwl/Makefile.am
1 1 #############################################################################
2 2 # Makefile.am for mwl module #
3 3 # ------------------------------------------------------------------------- #
4   -# Copyright (C) 2016-2018 Juergen Knoedlseder #
  4 +# Copyright (C) 2016-2021 Juergen Knoedlseder #
5 5 # ------------------------------------------------------------------------- #
6 6 # #
7 7 # This program is free software: you can redistribute it and/or modify #
... ... @@ -33,16 +33,14 @@ INCLUDES = -I$(top_srcdir)/include \
33 33 -I$(top_srcdir)/inst/mwl/include
34 34  
35 35 # Define sources for this directory
36   -sources = src/GMWLException.cpp \
37   - src/GMWLObservation.cpp \
  36 +sources = src/GMWLObservation.cpp \
38 37 src/GMWLSpectrum.cpp \
39 38 src/GMWLDatum.cpp \
40 39 src/GMWLInstDir.cpp \
41 40 src/GMWLResponse.cpp
42 41  
43 42 # Define headers to be installed
44   -pkginclude_HEADERS = include/GMWLException.hpp \
45   - include/GMWLObservation.hpp \
  43 +pkginclude_HEADERS = include/GMWLObservation.hpp \
46 44 include/GMWLSpectrum.hpp \
47 45 include/GMWLDatum.hpp \
48 46 include/GMWLInstDir.hpp \
... ...
inst/mwl/include/GMWLException.hpp deleted
1   -/***************************************************************************
2   - * GMWLException.hpp - Multi-wavelength exception handler *
3   - * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2011 by Jurgen Knodlseder *
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 GMWLException.hpp
23   - * @brief MWL exception handler interface definition.
24   - * @author J. Knodlseder
25   - */
26   -
27   -#ifndef GMWLEXCEPTION_HPP
28   -#define GMWLEXCEPTION_HPP
29   -
30   -/* __ Includes ___________________________________________________________ */
31   -#include <string> // string
32   -#include <sstream> // ostringstream
33   -#include <stdexcept> // exception
34   -
35   -
36   -/***********************************************************************//**
37   - * @class GMWLExceptionHandler
38   - *
39   - * @brief Interface for multi-wavelength exception handler
40   - ***************************************************************************/
41   -class GMWLExceptionHandler : public std::exception {
42   -public:
43   - GMWLExceptionHandler(void) { }
44   - virtual ~GMWLExceptionHandler(void) throw() { }
45   - virtual const char* what() const throw();
46   -protected:
47   - std::string m_origin;
48   - std::string m_message;
49   -};
50   -
51   -
52   -/***********************************************************************//**
53   - * @class GMWLException
54   - *
55   - * @brief Interface for multi-wavelength exceptions
56   - *
57   - * This object is thrown in case of a multi-wavelength exception.
58   - ***************************************************************************/
59   -class GMWLException : public GMWLExceptionHandler {
60   -public:
61   - // General exceptions
62   - class file_open_error : public GMWLExceptionHandler {
63   - public:
64   - file_open_error(std::string origin, std::string filename,
65   - std::string message = "");
66   - };
67   - class bad_file_format : public GMWLExceptionHandler {
68   - public:
69   - bad_file_format(std::string origin, std::string message = "");
70   - };
71   - class invalid_unit : public GMWLExceptionHandler {
72   - public:
73   - invalid_unit(std::string origin, std::string unit,
74   - std::string message = "");
75   - };
76   -
77   - // Observation exceptions
78   - class bad_response_type : public GMWLExceptionHandler {
79   - public:
80   - bad_response_type(std::string origin, std::string message = "");
81   - };
82   -};
83   -
84   -#endif /* GMWLEXCEPTION_HPP */
inst/mwl/include/GMWLLib.hpp
1 1 /***************************************************************************
2 2 * GMWLLib.hpp - Multi-wavelength Support Header files *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010 by Jurgen Knodlseder *
  4 + * copyright (C) 2010-2021 by Jurgen Knodlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -26,9 +26,10 @@
26 26 #include "GammaLib.hpp"
27 27  
28 28 /* __ MWL specific headers _______________________________________________ */
29   -#include "GMWLException.hpp"
  29 +#include "GMWLDatum.hpp"
  30 +#include "GMWLInstDir.hpp"
30 31 #include "GMWLObservation.hpp"
  32 +#include "GMWLResponse.hpp"
31 33 #include "GMWLSpectrum.hpp"
32   -#include "GMWLDatum.hpp"
33 34  
34 35 #endif /* GMWLLIB_HPP */
... ...
inst/mwl/src/GMWLException.cpp deleted
1   -/***************************************************************************
2   - * GMWLException.cpp - Multi-wavelength exception handler *
3   - * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2012 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 GMWLException.cpp
23   - * @brief MWL exception handler interface implementation.
24   - * @author J. Knoedlseder
25   - */
26   -
27   -/* __ Includes ___________________________________________________________ */
28   -#include "GMWLException.hpp"
29   -#include "GTools.hpp"
30   -
31   -
32   -/***********************************************************************//**
33   - * @brief Exception message
34   - ***************************************************************************/
35   -const char* GMWLExceptionHandler::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 Error while opening file
47   - *
48   - * @param[in] origin Name of method that has thrown the exception.
49   - * @param[in] filename Name of file that could not be opened.
50   - * @param[in] message Optional error message.
51   - ***************************************************************************/
52   -GMWLException::file_open_error::file_open_error(std::string origin,
53   - std::string filename,
54   - std::string message)
55   -{
56   - // Set origin
57   - m_origin = origin;
58   -
59   - // Set message
60   - m_message = "Unable to open file \""+filename+"\".";
61   -
62   - // Append optional message
63   - if (message.length() > 0) {
64   - m_message += " "+message;
65   - }
66   -
67   - // Return
68   - return;
69   -}
70   -
71   -
72   -/***********************************************************************//**
73   - * @brief File has invalid format
74   - *
75   - * @param[in] origin Name of method that has thrown the exception.
76   - * @param[in] message Optional error message.
77   - ***************************************************************************/
78   -GMWLException::bad_file_format::bad_file_format(std::string origin,
79   - std::string message)
80   -{
81   - // Set origin
82   - m_origin = origin;
83   -
84   - // Set message
85   - m_message = "File has invalid format.";
86   -
87   - // Append optional message
88   - if (message.length() > 0) {
89   - m_message += " "+message;
90   - }
91   -
92   - // Return
93   - return;
94   -}
95   -
96   -
97   -/***********************************************************************//**
98   - * @brief Invalid or unsupported unit encountered
99   - *
100   - * @param[in] origin Name of method that has thrown the exception.
101   - * @param[in] unit Unit string.
102   - * @param[in] message Optional error message.
103   - ***************************************************************************/
104   -GMWLException::invalid_unit::invalid_unit(std::string origin,
105   - std::string unit,
106   - std::string message)
107   -{
108   - // Set origin
109   - m_origin = origin;
110   -
111   - // Set message
112   - m_message = "Invalid or unsupported unit \""+unit+"\" encountered.";
113   -
114   - // Append optional message
115   - if (message.length() > 0) {
116   - m_message += " "+message;
117   - }
118   -
119   - // Return
120   - return;
121   -}
122   -
123   -
124   -/***********************************************************************//**
125   - * @brief Response is not a MWL response
126   - *
127   - * @param[in] origin Name of method that has thrown the exception.
128   - * @param[in] message Optional error message.
129   - ***************************************************************************/
130   -GMWLException::bad_response_type::bad_response_type(std::string origin,
131   - std::string message)
132   -{
133   - // Set origin
134   - m_origin = origin;
135   -
136   - // Set message
137   - m_message = "Response is not of type GMWLResponse.";
138   -
139   - // Append optional message
140   - if (message.length() > 0) {
141   - m_message += " "+message;
142   - }
143   -
144   - // Return
145   - return;
146   -}
inst/mwl/src/GMWLObservation.cpp
1 1 /***************************************************************************
2 2 * GMWLObservation.cpp - Multi-wavelength observation class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2017 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2021 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -34,7 +34,6 @@
34 34 #include "GException.hpp"
35 35 #include "GMWLObservation.hpp"
36 36 #include "GMWLSpectrum.hpp"
37   -#include "GMWLException.hpp"
38 37  
39 38 /* __ Globals ____________________________________________________________ */
40 39 const GMWLObservation g_obs_mwl_seed;
... ... @@ -205,8 +204,8 @@ GMWLObservation* GMWLObservation::clone(void) const
205 204 *
206 205 * @param[in] rsp Response function.
207 206 *
208   - * @exception GMWLException::bad_response_type
209   - * Specified response in not of type GMWLResponse.
  207 + * @exception GException::invalid_argument
  208 + * Response @p rsp in not a MWL response.
210 209 *
211 210 * Sets the response function for the observation. The argument has to be of
212 211 * type GMWLResponse, otherwise an exception is thrown.
... ... @@ -215,8 +214,14 @@ void GMWLObservation::response(const GResponse&amp; rsp)
215 214 {
216 215 // Get pointer on MWL response
217 216 const GMWLResponse* mwlrsp = dynamic_cast<const GMWLResponse*>(&rsp);
  217 +
  218 + // If pointer is not valid then throw an exception
218 219 if (mwlrsp == NULL) {
219   - throw GMWLException::bad_response_type(G_RESPONSE);
  220 + std::string cls = std::string(typeid(&rsp).name());
  221 + std::string msg = "Invalid response type \""+cls+"\" specified. "
  222 + "Please specify a \"GMWLResponse\" instance as "
  223 + "argument.";
  224 + throw GException::invalid_argument(G_RESPONSE, msg);
220 225 }
221 226  
222 227 // Copy response function
... ... @@ -270,11 +275,6 @@ void GMWLObservation::read(const GXmlElement&amp; xml)
270 275 *
271 276 * @param[in] xml XML element.
272 277 *
273   - * @exception GException::xml_invalid_parnum
274   - * Invalid number of parameters found in XML element.
275   - * @exception GException::xml_invalid_parnames
276   - * Invalid parameter names found in XML element.
277   - *
278 278 * Writes information for a multi-wavelength observation into an XML element.
279 279 * The format of the XML element is
280 280 *
... ...
inst/mwl/src/GMWLSpectrum.cpp
1 1 /***************************************************************************
2 2 * GMWLSpectrum.cpp - Multi-wavelength spectrum class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2016 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2021 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -34,7 +34,6 @@
34 34 #include "GFitsTable.hpp"
35 35 #include "GEnergy.hpp"
36 36 #include "GException.hpp"
37   -#include "GMWLException.hpp"
38 37 #include "GMWLSpectrum.hpp"
39 38  
40 39 /* __ Method name definitions ____________________________________________ */
... ... @@ -349,8 +348,8 @@ void GMWLSpectrum::read(const GFits&amp; fits, const std::string&amp; extname)
349 348 * @param[in] fits FITS file.
350 349 * @param[in] extno Extension number of spectrum.
351 350 *
352   - * @exception GMWLException::file_open_error
353   - * No table found in file.
  351 + * @exception GException::invalid_argument
  352 + * No table found in FITS file.
354 353 *
355 354 * Read the spectrum from a FITS table found in the specified extension.
356 355 * In no extension number if specified (or if extno=0) then the spectrum
... ... @@ -377,8 +376,10 @@ void GMWLSpectrum::read(const GFits&amp; fits, const int&amp; extno)
377 376  
378 377 // If we found no table then throw an exception
379 378 if (extension == 0) {
380   - throw GMWLException::file_open_error(G_READ, fits.filename().url(),
381   - "No table found in file.");
  379 + std::string msg = "No table found in file \""+fits.filename().url()+
  380 + "\". Please specify a FITS object containing a "
  381 + "table.";
  382 + throw GException::invalid_argument(G_READ, msg);
382 383 }
383 384  
384 385 // Get table pointer
... ... @@ -568,8 +569,8 @@ void GMWLSpectrum::set_ebounds(void)
568 569 *
569 570 * @param[in] table FITS table.
570 571 *
571   - * @exception GMWLException::bad_file_format
572   - * Table has invalid format
  572 + * @exception GException::invalid_argument
  573 + * Table does not contain at least two columns.
573 574 *
574 575 * Read spectrum from FITS table. The table is expected to be in one of the
575 576 * three following formats:
... ... @@ -608,9 +609,11 @@ void GMWLSpectrum::read_fits(const GFitsTable&amp; table)
608 609 c_flux_err = table[3];
609 610 }
610 611 else {
611   - throw GMWLException::bad_file_format(G_READ_FITS,
612   - "At least 2 columns are expected is table \""+
613   - table.extname()+"\".");
  612 + std::string msg = "At least 2 columns are expected in FITS table \""+
  613 + table.extname()+"\"but found "+
  614 + gammalib::str(table.ncols())+" columns. Please "
  615 + "specify a FITS table with at least two columns.";
  616 + throw GException::invalid_argument(G_READ_FITS, msg);
614 617 }
615 618  
616 619 // Read spectral points and add to spectrum
... ... @@ -682,8 +685,8 @@ GEnergy GMWLSpectrum::conv_energy(const double&amp; energy, const std::string&amp; unit)
682 685 * @param[in] flux Flux value.
683 686 * @param[in] unit Unit of value.
684 687 *
685   - * @exception GMWLException::invalid_unit
686   - * Invalid unit string encountered
  688 + * @exception GException::invalid_argument
  689 + * Invalid @p unit encountered.
687 690 *
688 691 * Converts a flux value into units of ph/cm2/s/MeV based on the specified
689 692 * units. The following units are supported (case insensitive):
... ... @@ -709,7 +712,10 @@ double GMWLSpectrum::conv_flux(const GEnergy&amp; energy, const double&amp; flux,
709 712  
710 713 // ... otherwise throw exception
711 714 else {
712   - throw GMWLException::invalid_unit(G_CONV_FLUX, unit);
  715 + std::string msg = "Unit \""+unit+"\" not recognised. Please specify "
  716 + "one of \"PH/CM2/S/MEV\", \"PH/S/CM2/MEV\", "
  717 + "\"ERG/CM2/S\" or \"ERG/S/CM2\".";
  718 + throw GException::invalid_argument(G_CONV_FLUX, msg);
713 719 }
714 720  
715 721 // Return energy
... ...