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 * Declares common exception classes for fatal error handling.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_utility
39 #ifndef GMX_UTILITY_EXCEPTIONS_H
40 #define GMX_UTILITY_EXCEPTIONS_H
45 #include <boost/exception/errinfo_api_function.hpp>
46 #include <boost/exception/errinfo_errno.hpp>
47 #include <boost/exception/exception.hpp>
48 #include <boost/exception/info.hpp>
49 #include <boost/throw_exception.hpp>
55 * Stores a user-friendly explanation for the reason of an exception.
57 * Typically, should not be used directly, but through the GromacsException
58 * class: it is initialized by the constructor, and can be accessed with
59 * GromacsException::what().
63 typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;
70 * Base class for all exception objects in Gromacs.
72 * Although boost recommends using virtual inheritance in exception hiearchies,
73 * it is not used here for two reasons:
74 * -# It is only useful when there is diamond inheritance, and that should
75 * never occur in this exception hierarchy because this class is the only
76 * instance of multiple inheritance (Gromacs programming guidelines prohibit
77 * multiple inheritance from concrete classes, but it is unavoidable here
78 * because of the design of boost::exception).
79 * -# Because the constructor takes an argument, virtual inheritance would
80 * complicate any classes that inherit indirectly from this class.
82 * \ingroup module_utility
84 class GromacsException : public std::exception, public boost::exception
88 * Returns the reason string for the exception.
90 * The return value is the string that was passed to the constructor.
92 virtual const char *what() const throw();
94 * Returns the error code corresponding to the exception type.
96 virtual int errorCode() const = 0;
100 * Creates an exception object with the provided detailed reason.
102 * \param[in] reason Detailed reason for the exception.
104 explicit GromacsException(const std::string &reason);
108 * Exception class for file I/O errors.
110 * \ingroup module_utility
112 class FileIOError : public GromacsException
116 * Creates an exception object with the provided detailed reason.
118 * \param[in] reason Detailed reason for the exception.
120 explicit FileIOError(const std::string &reason)
121 : GromacsException(reason) {}
123 virtual int errorCode() const;
127 * Exception class for user input errors.
129 * Derived classes should be used to indicate the nature of the error instead
130 * of throwing this class directly.
132 * \ingroup module_utility
134 class UserInputError : public GromacsException
137 //! \copydoc FileIOError::FileIOError()
138 explicit UserInputError(const std::string &reason)
139 : GromacsException(reason) {}
143 * Exception class for situations where user input cannot be parsed/understood.
145 * \ingroup module_utility
147 class InvalidInputError : public UserInputError
150 //! \copydoc FileIOError::FileIOError()
151 explicit InvalidInputError(const std::string &reason)
152 : UserInputError(reason) {}
154 virtual int errorCode() const;
158 * Exception class for situations where user input is inconsistent.
160 * \ingroup module_utility
162 class InconsistentInputError : public UserInputError
165 //! \copydoc FileIOError::FileIOError()
166 explicit InconsistentInputError(const std::string &reason)
167 : UserInputError(reason) {}
169 virtual int errorCode() const;
173 * Exception class for simulation instabilities.
175 * \ingroup module_utility
177 class SimulationInstabilityError : public GromacsException
180 //! \copydoc FileIOError::FileIOError()
181 explicit SimulationInstabilityError(const std::string &reason)
182 : GromacsException(reason) {}
184 virtual int errorCode() const;
188 * Exception class for internal errors.
190 * \ingroup module_utility
192 class InternalError : public GromacsException
195 //! \copydoc FileIOError::FileIOError()
196 explicit InternalError(const std::string &reason)
197 : GromacsException(reason) {}
199 virtual int errorCode() const;
203 * Exception class for incorrect use of an API.
205 * \ingroup module_utility
207 class APIError : public GromacsException
210 //! \copydoc FileIOError::FileIOError()
211 explicit APIError(const std::string &reason)
212 : GromacsException(reason) {}
214 virtual int errorCode() const;
218 * Exception class for use of an unimplemented feature.
220 * \ingroup module_utility
222 class NotImplementedError : public APIError
225 //! \copydoc FileIOError::FileIOError()
226 explicit NotImplementedError(const std::string &reason)
227 : APIError(reason) {}
229 virtual int errorCode() const;
234 * Macro for throwing an exception.
236 * \param[in] e Exception object to throw.
238 * Using this macro instead of \c throw directly makes it possible to uniformly
239 * attach information into the exception objects.
240 * \p e should evaluate to an instance of an object derived from
247 GMX_THROW(InconsistentUserInput("Negative values not allowed for value"));
251 #define GMX_THROW(e) \
252 BOOST_THROW_EXCEPTION((e))
255 * Macro for throwing an exception based on errno.
257 * \param[in] e Exception object to throw.
258 * \param[in] syscall Name of the syscall that returned the error.
259 * \param[in] err errno value returned by the syscall.
261 * This macro provides a convenience interface for throwing an exception to
262 * report an error based on a errno value. In addition to adding the necessary
263 * information to the exception object, the macro also ensures that \p errno is
264 * evaluated before, e.g., the constructor of \p e may call other functions
265 * that could overwrite the errno value.
266 * \p e should evaluate to an instance of an object derived from
269 * Typical usage (note that gmx::File wraps this particular case):
271 FILE *fp = fopen("filename.txt", "r");
274 GMX_THROW(FileIOError("Could not open file"), "fopen", errno);
278 #define GMX_THROW_WITH_ERRNO(e, syscall, err) \
280 int stored_errno_ = (err); \
281 GMX_THROW((e) << boost::errinfo_errno(stored_errno_) \
282 << boost::errinfo_api_function(syscall)); \
286 * Formats a standard error message for reporting an error.
288 * Normal usage in Gromacs command-line programs is like this:
290 int main(int argc, char *argv[])
294 // The actual code for the program
297 catch (const std::exception &ex)
299 fprintf(stderr, "%s", gmx::formatErrorMessage(ex).c_str());
305 std::string formatErrorMessage(const std::exception &ex);
308 * Converts an exception into a return code.
310 int translateException(const std::exception &ex);