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"
40 #include <boost/exception/get_error_info.hpp>
41 #include <boost/shared_ptr.hpp>
43 #include "gromacs/utility/errorcodes.h"
44 #include "gromacs/utility/gmxassert.h"
46 #include "errorformat.h"
54 /********************************************************************
62 * Creates an error message object with the specified text.
64 * \param[in] text Text for the message.
66 explicit ErrorMessage(const std::string &text);
68 //! Whether this object is a context string.
69 bool isContext() const { return child_; }
70 //! Returns the text for this object.
71 const std::string &text() const { return text_; }
73 * Returns the child object for a context object.
75 * Must not be called if isContext() returns false.
77 const ErrorMessage &child() const
79 GMX_ASSERT(isContext(),
80 "Attempting to access nonexistent message object");
85 * Creates a new message object with context prepended.
87 * \param[in] context Context string to add.
88 * \returns New error message object that has \p context as its text
89 * and \c this as its child.
90 * \throws std::bad_alloc if out of memory.
92 ErrorMessage prependContext(const std::string &context) const;
96 boost::shared_ptr<ErrorMessage> child_;
100 * Stores a reason or the top-most context string of an exception.
102 * \ingroup module_utility
104 typedef boost::error_info<struct errinfo_message_, ErrorMessage>
107 ErrorMessage::ErrorMessage(const std::string &text)
110 size_t length = text_.find_last_not_of(" \n");
111 if (length == std::string::npos)
113 length = text_.length() - 1;
115 text_.resize(length + 1);
119 ErrorMessage::prependContext(const std::string &context) const
121 ErrorMessage newMessage(context);
122 newMessage.child_.reset(new ErrorMessage(*this));
127 * Stores list of nested exceptions for Gromacs exceptions.
129 * \ingroup module_utility
131 typedef boost::error_info<struct errinfo_message_, internal::NestedExceptionList>
132 errinfo_nested_exceptions;
136 /********************************************************************
140 GromacsException::GromacsException(const ExceptionInitializer &details)
142 *this << errinfo_message(ErrorMessage(details.reason_));
143 if (details.hasNestedExceptions())
145 *this << errinfo_nested_exceptions(details.nested_);
149 const char *GromacsException::what() const throw()
151 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*this);
152 while (msg != NULL && msg->isContext())
156 return msg != NULL ? msg->text().c_str() : "No reason provided";
159 void GromacsException::prependContext(const std::string &context)
161 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*this);
162 GMX_RELEASE_ASSERT(msg != NULL, "Message should always be set");
163 *this << errinfo_message(msg->prependContext(context));
166 /********************************************************************
167 * Derived exception classes
170 int FileIOError::errorCode() const
175 int InvalidInputError::errorCode() const
177 return eeInvalidInput;
180 int InconsistentInputError::errorCode() const
182 return eeInconsistentInput;
185 int SimulationInstabilityError::errorCode() const
187 return eeInstability;
190 int InternalError::errorCode() const
192 return eeInternalError;
195 int APIError::errorCode() const
200 int NotImplementedError::errorCode() const
202 return eeNotImplemented;
206 /********************************************************************
213 void printExceptionMessage(FILE *fp, const std::exception &ex, int indent)
215 const boost::exception *boostEx = dynamic_cast<const boost::exception *>(&ex);
218 // TODO: Remove duplicate context if present in multiple nested exceptions.
219 const ErrorMessage *msg = boost::get_error_info<errinfo_message>(*boostEx);
222 while (msg != NULL && msg->isContext())
224 internal::printFatalErrorMessageLine(fp, msg->text().c_str(), indent*2);
228 if (msg != NULL && !msg->text().empty())
230 internal::printFatalErrorMessageLine(fp, msg->text().c_str(), indent*2);
235 internal::printFatalErrorMessageLine(fp, ex.what(), 0);
238 // TODO: Treat also boost::nested_exception (not currently used, though)
240 const internal::NestedExceptionList *nested
241 = boost::get_error_info<errinfo_nested_exceptions>(*boostEx);
245 internal::NestedExceptionList::const_iterator ni;
246 for (ni = nested->begin(); ni != nested->end(); ++ni)
250 rethrow_exception(*ni);
252 catch (const std::exception &nestedEx)
254 printExceptionMessage(fp, nestedEx, indent);
259 // TODO: Treat errno information in boost exceptions
263 internal::printFatalErrorMessageLine(fp, ex.what(), 0);
269 void printFatalErrorMessage(FILE *fp, const std::exception &ex)
271 const char *title = "Unknown exception";
272 const GromacsException *gmxEx = dynamic_cast<const GromacsException *>(&ex);
273 // TODO: Also treat common standard exceptions
276 title = getErrorCodeString(gmxEx->errorCode());
278 else if (dynamic_cast<const std::bad_alloc *>(&ex) != NULL)
280 title = "Memory allocation failed";
282 // We can't call get_error_info directly on ex since our internal boost
283 // needs to be compiled with BOOST_NO_RTTI. So we do the dynamic_cast
285 const char *const *funcPtr = NULL;
286 const char *const *filePtr = NULL;
287 const int *linePtr = NULL;
288 const boost::exception *boostEx = dynamic_cast<const boost::exception *>(&ex);
291 funcPtr = boost::get_error_info<boost::throw_function>(*boostEx);
292 filePtr = boost::get_error_info<boost::throw_file>(*boostEx);
293 linePtr = boost::get_error_info<boost::throw_line>(*boostEx);
295 internal::printFatalErrorHeader(fp, title,
296 funcPtr != NULL ? *funcPtr : NULL,
297 filePtr != NULL ? *filePtr : NULL,
298 linePtr != NULL ? *linePtr : 0);
299 printExceptionMessage(fp, ex, 0);
300 internal::printFatalErrorFooter(fp);