3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements classes and functions in exceptions.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_utility
38 #include "gromacs/utility/exceptions.h"
40 #include <boost/exception/get_error_info.hpp>
42 #include "gromacs/utility/errorcodes.h"
44 #include "errorformat.h"
49 /********************************************************************
53 GromacsException::GromacsException(const std::string &reason)
55 *this << errinfo_message(reason);
58 const char *GromacsException::what() const throw()
60 const std::string *msg = boost::get_error_info<errinfo_message>(*this);
61 return msg != NULL ? msg->c_str() : "No reason provided";
64 /********************************************************************
65 * Derived exception classes
68 int FileIOError::errorCode() const
73 int InvalidInputError::errorCode() const
75 return eeInvalidInput;
78 int InconsistentInputError::errorCode() const
80 return eeInconsistentInput;
83 int SimulationInstabilityError::errorCode() const
88 int InternalError::errorCode() const
90 return eeInternalError;
93 int APIError::errorCode() const
98 int NotImplementedError::errorCode() const
100 return eeNotImplemented;
104 /********************************************************************
108 void printFatalErrorMessage(FILE *fp, const std::exception &ex)
110 const char *title = "Unknown exception";
111 const GromacsException *gmxEx = dynamic_cast<const GromacsException *>(&ex);
112 // TODO: Also treat common standard exceptions
115 title = getErrorCodeString(gmxEx->errorCode());
117 else if (dynamic_cast<const std::bad_alloc *>(&ex) != NULL)
119 title = "Memory allocation failed";
121 // We can't call get_error_info directly on ex since our internal boost
122 // needs to be compiled with BOOST_NO_RTTI. So we do the dynamic_cast
124 const char *const *funcPtr = NULL;
125 const char *const *filePtr = NULL;
126 const int *linePtr = NULL;
127 const boost::exception *boostEx = dynamic_cast<const boost::exception *>(&ex);
130 funcPtr = boost::get_error_info<boost::throw_function>(*boostEx);
131 filePtr = boost::get_error_info<boost::throw_file>(*boostEx);
132 linePtr = boost::get_error_info<boost::throw_line>(*boostEx);
134 // TODO: Treat errno information in boost exceptions
135 internal::printFatalError(fp, title, ex.what(),
136 funcPtr != NULL ? *funcPtr : NULL,
137 filePtr != NULL ? *filePtr : NULL,
138 linePtr != NULL ? *linePtr : 0);