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 "exceptions.h"
46 #include <boost/exception/get_error_info.hpp>
47 #include <boost/shared_ptr.hpp>
49 #include "gromacs/legacyheaders/thread_mpi/system_error.h"
50 #include "gromacs/utility/errorcodes.h"
51 #include "gromacs/utility/gmxassert.h"
53 #include "errorformat.h"
61 /********************************************************************
69 * Creates an error message object with the specified text.
71 * \param[in] text Text for the message.
73 explicit ErrorMessage(const std::string &text);
75 //! Whether this object is a context string.
76 bool isContext() const { return child_; }
77 //! Returns the text for this object.
78 const std::string &text() const { return text_; }
80 * Returns the child object for a context object.
82 * Must not be called if isContext() returns false.
84 const ErrorMessage &child() const
86 GMX_ASSERT(isContext(),
87 "Attempting to access nonexistent message object");
92 * Creates a new message object with context prepended.
94 * \param[in] context Context string to add.
95 * \returns New error message object that has \p context as its text
96 * and \c this as its child.
97 * \throws std::bad_alloc if out of memory.
99 ErrorMessage prependContext(const std::string &context) const;
103 boost::shared_ptr<ErrorMessage> child_;
107 * Stores a reason or the top-most context string of an exception.
109 * \ingroup module_utility
111 typedef boost::error_info<struct errinfo_message_, ErrorMessage>
114 ErrorMessage::ErrorMessage(const std::string &text)
117 size_t length = text_.find_last_not_of(" \n");
118 if (length == std::string::npos)
120 length = text_.length() - 1;
122 text_.resize(length + 1);
126 ErrorMessage::prependContext(const std::string &context) const
128 ErrorMessage newMessage(context);
129 newMessage.child_.reset(new ErrorMessage(*this));
134 * Stores list of nested exceptions for Gromacs exceptions.
136 * \ingroup module_utility
138 typedef boost::error_info<struct errinfo_message_, internal::NestedExceptionList>
139 errinfo_nested_exceptions;
143 /********************************************************************
147 GromacsException::GromacsException(const ExceptionInitializer &details)
149 *this << errinfo_message(ErrorMessage(details.reason_));
150 if (details.hasNestedExceptions())
152 *this << errinfo_nested_exceptions(details.nested_);
156 const char *GromacsException::what() const throw()
158 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*this);
159 while (msg != NULL && msg->isContext())
163 return msg != NULL ? msg->text().c_str() : "No reason provided";
166 void GromacsException::prependContext(const std::string &context)
168 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*this);
169 GMX_RELEASE_ASSERT(msg != NULL, "Message should always be set");
170 *this << errinfo_message(msg->prependContext(context));
173 /********************************************************************
174 * Derived exception classes
177 int FileIOError::errorCode() const
182 int InvalidInputError::errorCode() const
184 return eeInvalidInput;
187 int InconsistentInputError::errorCode() const
189 return eeInconsistentInput;
192 int SimulationInstabilityError::errorCode() const
194 return eeInstability;
197 int InternalError::errorCode() const
199 return eeInternalError;
202 int APIError::errorCode() const
207 int NotImplementedError::errorCode() const
209 return eeNotImplemented;
213 /********************************************************************
221 * Prints error information for an exception object.
223 * \param[in] fp File to write the information out to (typically stderr).
224 * \param[in] ex Exception object to print.
225 * \param[in] indent Indentation for the information.
227 * If the exception contains nested exceptions, information from them is
228 * recursively printed.
230 void printExceptionMessage(FILE *fp, const std::exception &ex, int indent)
232 const boost::exception *boostEx = dynamic_cast<const boost::exception *>(&ex);
235 // TODO: Remove duplicate context if present in multiple nested exceptions.
236 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*boostEx);
239 while (msg != NULL && msg->isContext())
241 internal::printFatalErrorMessageLine(fp, msg->text().c_str(), indent*2);
245 if (msg != NULL && !msg->text().empty())
247 internal::printFatalErrorMessageLine(fp, msg->text().c_str(), indent*2);
252 internal::printFatalErrorMessageLine(fp, ex.what(), indent);
255 const int *errorNumber
256 = boost::get_error_info<boost::errinfo_errno>(*boostEx);
257 if (errorNumber != NULL)
259 std::fprintf(fp, "%*sReason: %s\n", (indent+1)*2, "",
260 std::strerror(*errorNumber));
261 const char * const *funcName
262 = boost::get_error_info<boost::errinfo_api_function>(*boostEx);
263 if (funcName != NULL)
265 std::fprintf(fp, "%*s(call to %s() returned error code %d)\n",
266 (indent+1)*2, "", *funcName, *errorNumber);
270 // TODO: Treat also boost::nested_exception (not currently used, though)
272 const internal::NestedExceptionList *nested
273 = boost::get_error_info<errinfo_nested_exceptions>(*boostEx);
276 internal::NestedExceptionList::const_iterator ni;
277 for (ni = nested->begin(); ni != nested->end(); ++ni)
281 rethrow_exception(*ni);
283 catch (const std::exception &nestedEx)
285 printExceptionMessage(fp, nestedEx, indent + 1);
292 internal::printFatalErrorMessageLine(fp, ex.what(), indent);
298 void printFatalErrorMessage(FILE *fp, const std::exception &ex)
300 const char *title = "Unknown exception";
301 bool bPrintType = false;
302 const GromacsException *gmxEx = dynamic_cast<const GromacsException *>(&ex);
303 // TODO: Treat more of the standard exceptions
306 title = getErrorCodeString(gmxEx->errorCode());
308 else if (dynamic_cast<const tMPI::system_error *>(&ex) != NULL)
310 title = "System error in thread synchronization";
312 else if (dynamic_cast<const std::bad_alloc *>(&ex) != NULL)
314 title = "Memory allocation failed";
316 else if (dynamic_cast<const std::logic_error *>(&ex) != NULL)
318 title = "Standard library logic error (bug)";
321 else if (dynamic_cast<const std::runtime_error *>(&ex) != NULL)
323 title = "Standard library runtime error (possible bug)";
330 // We can't call get_error_info directly on ex since our internal boost
331 // needs to be compiled with BOOST_NO_RTTI. So we do the dynamic_cast
333 const char *const *funcPtr = NULL;
334 const char *const *filePtr = NULL;
335 const int *linePtr = NULL;
336 const boost::exception *boostEx = dynamic_cast<const boost::exception *>(&ex);
339 funcPtr = boost::get_error_info<boost::throw_function>(*boostEx);
340 filePtr = boost::get_error_info<boost::throw_file>(*boostEx);
341 linePtr = boost::get_error_info<boost::throw_line>(*boostEx);
343 internal::printFatalErrorHeader(fp, title,
344 funcPtr != NULL ? *funcPtr : NULL,
345 filePtr != NULL ? *filePtr : NULL,
346 linePtr != NULL ? *linePtr : 0);
349 std::fprintf(fp, "(exception type: %s)\n", typeid(ex).name());
351 printExceptionMessage(fp, ex, 0);
352 internal::printFatalErrorFooter(fp);