refactor exceptions and exceptions hierarchy

This commit is contained in:
Johann Dreo 2020-03-26 07:53:23 +01:00
commit 3b01169726
2 changed files with 106 additions and 59 deletions

View file

@ -28,8 +28,63 @@ Johann Dréo <johann.dreo@thalesgroup.com>
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
class eoMaxException : public std::exception {}; //! You can catch this base exception if you want to catch anything thrown by ParadisEO. @ingroup Core
class eoException : public std::runtime_error
{
public:
eoException(std::string msg = "") :
std::runtime_error(msg)
{
_msg << msg;
}
const char* what() const throw()
{
return _msg.str().c_str();
}
~eoException() throw() {}
protected:
std::ostringstream _msg;
};
/** Base class for exceptions which need to stop the algorithm to be handled
*
* (like stopping criterion or numerical errors).
*/
class eoStopException : public eoException
{
public:
eoStopException(std::string msg = "") : eoException(msg) {}
~eoStopException() throw() {}
};
//! Base class for limit-based exceptions (see eoMaxTimeException and eoMaxEvalException.
class eoMaxException : public eoStopException
{
public:
eoMaxException(std::string msg = "") : eoStopException(msg) {}
~eoMaxException() throw() {}
};
/*!
An error that signals that some bad data have been returned.
Thrown by @see eoEvalNanThrowException
@ingroup Evaluation
*/
class eoNanException : public eoStopException
{
public:
eoNanException() :
eoStopException("The objective function returned a bad value (nan or inf)")
{ }
~eoNanException() throw() {}
};
/*! /*!
@ -42,43 +97,41 @@ Thrown by @see eoEvalTimeThrowException
class eoMaxTimeException : public eoMaxException class eoMaxTimeException : public eoMaxException
{ {
public: public:
eoMaxTimeException( time_t elapsed) : _elapsed(elapsed) {} eoMaxTimeException( time_t elapsed) :
eoMaxException("STOP")
virtual const char* what() const throw()
{ {
std::ostringstream ss; _msg << " the maximum number of allowed seconds has been reached ("
ss << "STOP in eoMaxTimeException: the maximum number of allowed seconds has been reached (" << _elapsed << ")."; << elapsed << ")";
return ss.str().c_str();
} }
~eoMaxTimeException() throw() {}
private:
const time_t _elapsed;
}; };
/*! /*!
An error that signals that a maximum number of evaluations has been reached. An error that signals that a maximum number of evaluations has been reached.
Thrown by @see eoEvalEvalThrowException Thrown by @see eoEvalThrowException
@ingroup Evaluation @ingroup Evaluation
*/ */
class eoMaxEvalException : public eoMaxException class eoMaxEvalException : public eoMaxException
{ {
public: public:
eoMaxEvalException(unsigned long threshold) : _threshold(threshold){} eoMaxEvalException(unsigned long threshold) :
eoMaxException("STOP")
virtual const char* what() const throw()
{ {
std::ostringstream ss; _msg << " the maximum number of evaluation has been reached ("
ss << "STOP in eoMaxEvalException: the maximum number of evaluation has been reached (" << _threshold << ")."; << threshold << ").";
return ss.str().c_str();
} }
~eoMaxEvalException() throw() {}
private:
const unsigned long _threshold;
}; };
//! Base class for exceptions related to eoParam management. @ingroup Parameters
class eoParamException : public eoException
{
public:
eoParamException(std::string msg = "") : eoException(msg) {}
};
/*! /*!
* An error that signals a missing parameter * An error that signals a missing parameter
@ -87,24 +140,18 @@ private:
* *
* @ingroup Parameters * @ingroup Parameters
*/ */
class eoMissingParamException : public std::exception class eoMissingParamException : public eoParamException
{ {
public: public:
eoMissingParamException(std::string name) : _name(name){} eoMissingParamException(std::string name) :
eoParamException()
virtual const char* what() const throw()
{ {
std::ostringstream ss; _msg << "The command parameter " << name << " has not been declared";
ss << "The command parameter " << _name << " has not been declared";
return ss.str().c_str();
} }
~eoMissingParamException() throw() {} ~eoMissingParamException() throw() {}
private:
const std::string _name;
}; };
/*! /*!
* An error that signals a bad parameter type * An error that signals a bad parameter type
* *
@ -112,37 +159,41 @@ private:
* *
* @ingroup Parameters * @ingroup Parameters
*/ */
class eoWrongParamTypeException : public std::exception class eoWrongParamTypeException : public eoParamException
{ {
public: public:
eoWrongParamTypeException(std::string name) : _name(name){} eoWrongParamTypeException(std::string name) :
eoParamException()
virtual const char* what() const throw()
{ {
std::ostringstream ss; _msg << "You asked for the parameter " << name
ss << "You asked for the parameter " << _name << " but it has not been declared under this type"; << " but it has not been declared under this type";
return ss.str().c_str();
} }
~eoWrongParamTypeException() throw() {} ~eoWrongParamTypeException() throw() {}
private:
const std::string _name;
}; };
class eoSystemError : public std::exception //! Exception related to a system call.
class eoSystemError : public eoException
{ {
public: public:
eoSystemError(std::string cmd) eoSystemError(std::string cmd) :
: _cmd(cmd), _has_pipe(false), _err_code(-1), _output("") eoException(),
{} _cmd(cmd), _has_pipe(false), _err_code(-1), _output("")
{
_msg << msg();
}
eoSystemError(std::string cmd, int err_code, std::string output) eoSystemError(std::string cmd, int err_code, std::string output) :
: _cmd(cmd), _has_pipe(true), _err_code(err_code), _output(output) eoException(),
{} _cmd(cmd), _has_pipe(true), _err_code(err_code), _output(output)
{
_msg << msg();
}
virtual const char* what() const throw() ~eoSystemError() throw() {}
protected:
const std::string msg() const throw()
{ {
std::ostringstream ss; std::ostringstream ss;
ss << "System call: `" << _cmd << "` error"; ss << "System call: `" << _cmd << "` error";
@ -150,17 +201,13 @@ public:
ss << " code #" << _err_code ss << " code #" << _err_code
<< " with the following output:" << std::endl << _output; << " with the following output:" << std::endl << _output;
} }
return ss.str().c_str(); return ss.str();
} }
~eoSystemError() throw() {}
private:
const std::string _cmd; const std::string _cmd;
const bool _has_pipe; const bool _has_pipe;
const int _err_code; const int _err_code;
const std::string _output; const std::string _output;
}; };
#endif // __eoExceptions_h__ #endif // __eoExceptions_h__

View file

@ -122,9 +122,9 @@ public :
/** /**
* Loading error thrown when nothing seems to work. * Loading error thrown when nothing seems to work.
*/ */
struct loading_error : public std::runtime_error struct loading_error : public eoException
{ {
loading_error(std::string huh = "Error while loading") : std::runtime_error(huh) {} loading_error(std::string huh = "Error while loading") : eoException(huh) {}
}; };
std::string getCommentString(void) const { return "#"; } std::string getCommentString(void) const { return "#"; }