Class to deal with Plumed runtime errors. More...
#include <Exception.h>
Public Member Functions | |
Exception () | |
Without message. More... | |
Exception (const std::string &) | |
With message. More... | |
Exception (const std::string &, const std::string &, unsigned, const std::string &) | |
With message plus file, line and function (meant to be used through a preprocessor macro) More... | |
virtual const char * | what () const throw () |
Returns the error message. More... | |
virtual | ~Exception () throw () |
Destructor should be defined and should not throw other exceptions. More... | |
Private Member Functions | |
void | abortIfExceptionsAreDisabled () |
Method which aborts in case exceptions are disabled. More... | |
Static Private Member Functions | |
static std::string | format (const std::string &, const std::string &, unsigned, const std::string &) |
Common tool, invoked by all the constructor to build the message string. More... | |
Private Attributes | |
std::string | msg |
Related Functions | |
(Note that these are not member functions.) | |
#define | plumed_error() throw PLMD::Exception("",__FILE__,__LINE__,__PRETTY_FUNCTION__) |
#define | plumed_merror(msg) throw PLMD::Exception(msg,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
#define | plumed_assert(test) if(!(test)) throw PLMD::Exception("assertion failed " #test,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
#define | plumed_massert(test, msg) if(!(test)) throw PLMD::Exception("assertion failed " #test ", " msg,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
#define | plumed_dbg_assert(test) if(!(test)) throw PLMD::Exception("assertion failed " #test,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
#define | plumed_dbg_massert(test, msg) if(!(test)) throw PLMD::Exception("assertion failed " #test ", " msg,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
Class to deal with Plumed runtime errors.
This class, or better the related macros, can be used to detect programming errors. Typical cases are internal inconsistencies or errors in the plumed<->MD interface. Mistakes made by final users (i.e. in the plumed.dat file) should be documented in some better way (e.g. printing parts of the manual in the output).
To throw an error, just throw a c++ exception
if(something_bad) throw Exception();
or better add an error message to that
if(something_bad) throw Exception("describe the error here);
Even better, you can use the predefined macros plumed_error(), plumed_assert(), plumed_merror() and plumed_massert(), which add information about the exact location of the error in the file (filename, line and, for g++, function name). Macros ending in "error" unconditionally throw the exception, whereas macros ending in "assert" first perform a conditional check (similarly to standard assert()). The extra "m" in the name means that an extensive error message can be added.
// examples: plumed_assert(a>0); plumed_massert(a>0,"a should be larger than zero"); if(a<=0) plumed_error(); if(a<=0) plumed_merror("a should be larger than zero");
The additional macros plumed_dbg_assert() and plumed_dbg_massert() are similar to plumed_assert() and plumed_massert() respectively, but the corresponding check is only performed when NDEBUG macro is not defined. They should be used when the check is expensive and should be skipped in production code.
By default, execution is terminated imediately and a message is printed on stderr.
If PLUMED is compiled with -D__PLUMED_EXCEPTIONS execution will continue and the exception will be passed to c++, so that it will be possible to intercepted it at a higher level, even outside plumed. E.g., in an external c++ code using PLUMED as a library, one can type
try{ plumed.cmd("setPrecision",n); } catch (std::exception & e) { printf("ee %s",e.what()); exit(1); }
This can be useful if an external code wants to exit in a controlled manner (e.g. flushing files, printing the error message in a specific file, etc.) but is anyway limited to c++ codes. Moreover, since these errors are expected to be unrecoverable, the MD code will usually not be able to do something more clever than exiting.
PLMD::Exception::Exception | ( | ) |
Without message.
PLMD::Exception::Exception | ( | const std::string & | msg | ) |
With message.
PLMD::Exception::Exception | ( | const std::string & | msg, |
const std::string & | file, | ||
unsigned | line, | ||
const std::string & | function | ||
) |
With message plus file, line and function (meant to be used through a preprocessor macro)
|
inlinevirtual |
Destructor should be defined and should not throw other exceptions.
|
private |
Method which aborts in case exceptions are disabled.
|
staticprivate |
Common tool, invoked by all the constructor to build the message string.
|
inlinevirtual |
Returns the error message.
|
related |
Conditionally print file/line/function information and exit
|
related |
Conditionally print file/line/function information and exit when NDEBUG flag is not present
|
related |
Conditionally print file/line/function information plus msg and exit when NDEBUG flag is not present
|
related |
Just print file/line/function information and exit
|
related |
Conditionally print file/line/function information plus msg and exit
|
related |
Print file/line/function information plus msg and exit
|
private |
Hosted by GitHub | 1.8.7 |