From 7b096714c6bbd793989afff6978d99e40d3f38b4 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Fri, 21 May 2021 04:05:32 +0200 Subject: [PATCH] Replace specific by generic exceptions in MWL module (#35) --- ChangeLog | 3 ++- NEWS | 5 +++-- README.md | 2 +- doc/source/admin/release_history/2.0.rst | 6 ++++-- inst/mwl/Makefile.am | 8 +++----- inst/mwl/include/GMWLException.hpp | 84 ------------------------------------------------------------------------------------ inst/mwl/include/GMWLLib.hpp | 7 ++++--- inst/mwl/src/GMWLException.cpp | 146 -------------------------------------------------------------------------------------------------------------------------------------------------- inst/mwl/src/GMWLObservation.cpp | 20 ++++++++++---------- inst/mwl/src/GMWLSpectrum.cpp | 34 ++++++++++++++++++++-------------- 10 files changed, 47 insertions(+), 268 deletions(-) delete mode 100644 inst/mwl/include/GMWLException.hpp delete mode 100644 inst/mwl/src/GMWLException.cpp diff --git a/ChangeLog b/ChangeLog index aad9434..10ca649 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -2021-05-10 +2021-05-21 * Version 2.0.0 released ======================== @@ -53,6 +53,7 @@ Add GSkyMap::flux(GRegion(s)) and GSkyMap::solidangle(GRegion(s)) methods (#1785) Reimplement GModelSpatial::flux() method for diffuse and combined models (#1785) Implement pre-computation cache in GModelSpatialGauss (#1300) + Replace specific by generic exceptions (#35) 2021-04-28 diff --git a/NEWS b/NEWS index a3bc469..691c4a2 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,6 @@ New Features and Important Changes in GammaLib 2.0.0 -10 May 2021 +21 May 2021 1. Introduction @@ -24,7 +24,8 @@ The following classes have been added: - GCTABackground2D The following classes have been removed: -- none +- GMWLExceptionHandler +- GMWLException The following classes have been renamed: - none diff --git a/README.md b/README.md index f247bde..65a6b06 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ GammaLib information ==================== -* Version: 2.0.0.dev (10 May 2021) +* Version: 2.0.0.dev (21 May 2021) [![Build Status](https://cta-jenkins.irap.omp.eu/buildStatus/icon?job=gammalib-integrate-os)](https://cta-jenkins.irap.omp.eu/job/gammalib-integrate-os/) diff --git a/doc/source/admin/release_history/2.0.rst b/doc/source/admin/release_history/2.0.rst index f4947f5..9e8854d 100644 --- a/doc/source/admin/release_history/2.0.rst +++ b/doc/source/admin/release_history/2.0.rst @@ -1,7 +1,7 @@ .. _2.0: -GammaLib 2.0 release (8 May 2021) -================================= +GammaLib 2.0 release (21 May 2021) +================================== Overview -------- @@ -120,3 +120,5 @@ Clean up Store DRB, DRG and DRX as instances of ``GCOMDri`` instead of ``GSkyMap`` in ``GCOMObservation`` * [`2973 `_] - Remove COMPTEL internal response caching +* [`35 `_] - + Replace specific by generic exceptions diff --git a/inst/mwl/Makefile.am b/inst/mwl/Makefile.am index 2df80fc..a1253db 100644 --- a/inst/mwl/Makefile.am +++ b/inst/mwl/Makefile.am @@ -1,7 +1,7 @@ ############################################################################# # Makefile.am for mwl module # # ------------------------------------------------------------------------- # -# Copyright (C) 2016-2018 Juergen Knoedlseder # +# Copyright (C) 2016-2021 Juergen Knoedlseder # # ------------------------------------------------------------------------- # # # # This program is free software: you can redistribute it and/or modify # @@ -33,16 +33,14 @@ INCLUDES = -I$(top_srcdir)/include \ -I$(top_srcdir)/inst/mwl/include # Define sources for this directory -sources = src/GMWLException.cpp \ - src/GMWLObservation.cpp \ +sources = src/GMWLObservation.cpp \ src/GMWLSpectrum.cpp \ src/GMWLDatum.cpp \ src/GMWLInstDir.cpp \ src/GMWLResponse.cpp # Define headers to be installed -pkginclude_HEADERS = include/GMWLException.hpp \ - include/GMWLObservation.hpp \ +pkginclude_HEADERS = include/GMWLObservation.hpp \ include/GMWLSpectrum.hpp \ include/GMWLDatum.hpp \ include/GMWLInstDir.hpp \ diff --git a/inst/mwl/include/GMWLException.hpp b/inst/mwl/include/GMWLException.hpp deleted file mode 100644 index 21a8962..0000000 --- a/inst/mwl/include/GMWLException.hpp +++ /dev/null @@ -1,84 +0,0 @@ -/*************************************************************************** - * GMWLException.hpp - Multi-wavelength exception handler * - * ----------------------------------------------------------------------- * - * copyright (C) 2010-2011 by Jurgen Knodlseder * - * ----------------------------------------------------------------------- * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 3 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - * * - ***************************************************************************/ -/** - * @file GMWLException.hpp - * @brief MWL exception handler interface definition. - * @author J. Knodlseder - */ - -#ifndef GMWLEXCEPTION_HPP -#define GMWLEXCEPTION_HPP - -/* __ Includes ___________________________________________________________ */ -#include // string -#include // ostringstream -#include // exception - - -/***********************************************************************//** - * @class GMWLExceptionHandler - * - * @brief Interface for multi-wavelength exception handler - ***************************************************************************/ -class GMWLExceptionHandler : public std::exception { -public: - GMWLExceptionHandler(void) { } - virtual ~GMWLExceptionHandler(void) throw() { } - virtual const char* what() const throw(); -protected: - std::string m_origin; - std::string m_message; -}; - - -/***********************************************************************//** - * @class GMWLException - * - * @brief Interface for multi-wavelength exceptions - * - * This object is thrown in case of a multi-wavelength exception. - ***************************************************************************/ -class GMWLException : public GMWLExceptionHandler { -public: - // General exceptions - class file_open_error : public GMWLExceptionHandler { - public: - file_open_error(std::string origin, std::string filename, - std::string message = ""); - }; - class bad_file_format : public GMWLExceptionHandler { - public: - bad_file_format(std::string origin, std::string message = ""); - }; - class invalid_unit : public GMWLExceptionHandler { - public: - invalid_unit(std::string origin, std::string unit, - std::string message = ""); - }; - - // Observation exceptions - class bad_response_type : public GMWLExceptionHandler { - public: - bad_response_type(std::string origin, std::string message = ""); - }; -}; - -#endif /* GMWLEXCEPTION_HPP */ diff --git a/inst/mwl/include/GMWLLib.hpp b/inst/mwl/include/GMWLLib.hpp index 64923ca..af7c6c1 100644 --- a/inst/mwl/include/GMWLLib.hpp +++ b/inst/mwl/include/GMWLLib.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GMWLLib.hpp - Multi-wavelength Support Header files * * ----------------------------------------------------------------------- * - * copyright (C) 2010 by Jurgen Knodlseder * + * copyright (C) 2010-2021 by Jurgen Knodlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -26,9 +26,10 @@ #include "GammaLib.hpp" /* __ MWL specific headers _______________________________________________ */ -#include "GMWLException.hpp" +#include "GMWLDatum.hpp" +#include "GMWLInstDir.hpp" #include "GMWLObservation.hpp" +#include "GMWLResponse.hpp" #include "GMWLSpectrum.hpp" -#include "GMWLDatum.hpp" #endif /* GMWLLIB_HPP */ diff --git a/inst/mwl/src/GMWLException.cpp b/inst/mwl/src/GMWLException.cpp deleted file mode 100644 index 71c93a9..0000000 --- a/inst/mwl/src/GMWLException.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/*************************************************************************** - * GMWLException.cpp - Multi-wavelength exception handler * - * ----------------------------------------------------------------------- * - * copyright (C) 2010-2012 by Juergen Knoedlseder * - * ----------------------------------------------------------------------- * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 3 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - * * - ***************************************************************************/ -/** - * @file GMWLException.cpp - * @brief MWL exception handler interface implementation. - * @author J. Knoedlseder - */ - -/* __ Includes ___________________________________________________________ */ -#include "GMWLException.hpp" -#include "GTools.hpp" - - -/***********************************************************************//** - * @brief Exception message - ***************************************************************************/ -const char* GMWLExceptionHandler::what() const throw() -{ - // Set error message - std::string message = "*** ERROR in " + m_origin + ": " + m_message; - - // Return message as C character array - return gammalib::tochar(message); -} - - -/***********************************************************************//** - * @brief Error while opening file - * - * @param[in] origin Name of method that has thrown the exception. - * @param[in] filename Name of file that could not be opened. - * @param[in] message Optional error message. - ***************************************************************************/ -GMWLException::file_open_error::file_open_error(std::string origin, - std::string filename, - std::string message) -{ - // Set origin - m_origin = origin; - - // Set message - m_message = "Unable to open file \""+filename+"\"."; - - // Append optional message - if (message.length() > 0) { - m_message += " "+message; - } - - // Return - return; -} - - -/***********************************************************************//** - * @brief File has invalid format - * - * @param[in] origin Name of method that has thrown the exception. - * @param[in] message Optional error message. - ***************************************************************************/ -GMWLException::bad_file_format::bad_file_format(std::string origin, - std::string message) -{ - // Set origin - m_origin = origin; - - // Set message - m_message = "File has invalid format."; - - // Append optional message - if (message.length() > 0) { - m_message += " "+message; - } - - // Return - return; -} - - -/***********************************************************************//** - * @brief Invalid or unsupported unit encountered - * - * @param[in] origin Name of method that has thrown the exception. - * @param[in] unit Unit string. - * @param[in] message Optional error message. - ***************************************************************************/ -GMWLException::invalid_unit::invalid_unit(std::string origin, - std::string unit, - std::string message) -{ - // Set origin - m_origin = origin; - - // Set message - m_message = "Invalid or unsupported unit \""+unit+"\" encountered."; - - // Append optional message - if (message.length() > 0) { - m_message += " "+message; - } - - // Return - return; -} - - -/***********************************************************************//** - * @brief Response is not a MWL response - * - * @param[in] origin Name of method that has thrown the exception. - * @param[in] message Optional error message. - ***************************************************************************/ -GMWLException::bad_response_type::bad_response_type(std::string origin, - std::string message) -{ - // Set origin - m_origin = origin; - - // Set message - m_message = "Response is not of type GMWLResponse."; - - // Append optional message - if (message.length() > 0) { - m_message += " "+message; - } - - // Return - return; -} diff --git a/inst/mwl/src/GMWLObservation.cpp b/inst/mwl/src/GMWLObservation.cpp index 29fd1d3..3c55c32 100644 --- a/inst/mwl/src/GMWLObservation.cpp +++ b/inst/mwl/src/GMWLObservation.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GMWLObservation.cpp - Multi-wavelength observation class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2017 by Juergen Knoedlseder * + * copyright (C) 2010-2021 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -34,7 +34,6 @@ #include "GException.hpp" #include "GMWLObservation.hpp" #include "GMWLSpectrum.hpp" -#include "GMWLException.hpp" /* __ Globals ____________________________________________________________ */ const GMWLObservation g_obs_mwl_seed; @@ -205,8 +204,8 @@ GMWLObservation* GMWLObservation::clone(void) const * * @param[in] rsp Response function. * - * @exception GMWLException::bad_response_type - * Specified response in not of type GMWLResponse. + * @exception GException::invalid_argument + * Response @p rsp in not a MWL response. * * Sets the response function for the observation. The argument has to be of * type GMWLResponse, otherwise an exception is thrown. @@ -215,8 +214,14 @@ void GMWLObservation::response(const GResponse& rsp) { // Get pointer on MWL response const GMWLResponse* mwlrsp = dynamic_cast(&rsp); + + // If pointer is not valid then throw an exception if (mwlrsp == NULL) { - throw GMWLException::bad_response_type(G_RESPONSE); + std::string cls = std::string(typeid(&rsp).name()); + std::string msg = "Invalid response type \""+cls+"\" specified. " + "Please specify a \"GMWLResponse\" instance as " + "argument."; + throw GException::invalid_argument(G_RESPONSE, msg); } // Copy response function @@ -270,11 +275,6 @@ void GMWLObservation::read(const GXmlElement& xml) * * @param[in] xml XML element. * - * @exception GException::xml_invalid_parnum - * Invalid number of parameters found in XML element. - * @exception GException::xml_invalid_parnames - * Invalid parameter names found in XML element. - * * Writes information for a multi-wavelength observation into an XML element. * The format of the XML element is * diff --git a/inst/mwl/src/GMWLSpectrum.cpp b/inst/mwl/src/GMWLSpectrum.cpp index f868593..2430d7b 100644 --- a/inst/mwl/src/GMWLSpectrum.cpp +++ b/inst/mwl/src/GMWLSpectrum.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GMWLSpectrum.cpp - Multi-wavelength spectrum class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2016 by Juergen Knoedlseder * + * copyright (C) 2010-2021 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -34,7 +34,6 @@ #include "GFitsTable.hpp" #include "GEnergy.hpp" #include "GException.hpp" -#include "GMWLException.hpp" #include "GMWLSpectrum.hpp" /* __ Method name definitions ____________________________________________ */ @@ -349,8 +348,8 @@ void GMWLSpectrum::read(const GFits& fits, const std::string& extname) * @param[in] fits FITS file. * @param[in] extno Extension number of spectrum. * - * @exception GMWLException::file_open_error - * No table found in file. + * @exception GException::invalid_argument + * No table found in FITS file. * * Read the spectrum from a FITS table found in the specified extension. * In no extension number if specified (or if extno=0) then the spectrum @@ -377,8 +376,10 @@ void GMWLSpectrum::read(const GFits& fits, const int& extno) // If we found no table then throw an exception if (extension == 0) { - throw GMWLException::file_open_error(G_READ, fits.filename().url(), - "No table found in file."); + std::string msg = "No table found in file \""+fits.filename().url()+ + "\". Please specify a FITS object containing a " + "table."; + throw GException::invalid_argument(G_READ, msg); } // Get table pointer @@ -568,8 +569,8 @@ void GMWLSpectrum::set_ebounds(void) * * @param[in] table FITS table. * - * @exception GMWLException::bad_file_format - * Table has invalid format + * @exception GException::invalid_argument + * Table does not contain at least two columns. * * Read spectrum from FITS table. The table is expected to be in one of the * three following formats: @@ -608,9 +609,11 @@ void GMWLSpectrum::read_fits(const GFitsTable& table) c_flux_err = table[3]; } else { - throw GMWLException::bad_file_format(G_READ_FITS, - "At least 2 columns are expected is table \""+ - table.extname()+"\"."); + std::string msg = "At least 2 columns are expected in FITS table \""+ + table.extname()+"\"but found "+ + gammalib::str(table.ncols())+" columns. Please " + "specify a FITS table with at least two columns."; + throw GException::invalid_argument(G_READ_FITS, msg); } // Read spectral points and add to spectrum @@ -682,8 +685,8 @@ GEnergy GMWLSpectrum::conv_energy(const double& energy, const std::string& unit) * @param[in] flux Flux value. * @param[in] unit Unit of value. * - * @exception GMWLException::invalid_unit - * Invalid unit string encountered + * @exception GException::invalid_argument + * Invalid @p unit encountered. * * Converts a flux value into units of ph/cm2/s/MeV based on the specified * units. The following units are supported (case insensitive): @@ -709,7 +712,10 @@ double GMWLSpectrum::conv_flux(const GEnergy& energy, const double& flux, // ... otherwise throw exception else { - throw GMWLException::invalid_unit(G_CONV_FLUX, unit); + std::string msg = "Unit \""+unit+"\" not recognised. Please specify " + "one of \"PH/CM2/S/MEV\", \"PH/S/CM2/MEV\", " + "\"ERG/CM2/S\" or \"ERG/S/CM2\"."; + throw GException::invalid_argument(G_CONV_FLUX, msg); } // Return energy -- libgit2 0.21.4