Dijkstra documentation is getting ready, but one decision is missing about naming conventions about named_params
2 * src/lemon/error.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
22 //! \brief Basic exception classes and error handling.
30 #include <boost/shared_ptr.hpp>
34 /// Exception-safe convenient "error message" class.
36 /// Helper class which provides a convenient ostream-like (operator <<
37 /// based) interface to create a string message. Mostly useful in
38 /// exception classes (therefore the name).
42 boost::shared_ptr<std::ostringstream> buf;
47 buf.reset(new std::ostringstream);
58 ErrorMessage() throw() { init(); }
61 ErrorMessage(const char *message) throw() {
67 ErrorMessage(const std::string &message) throw() {
74 ErrorMessage& operator<<(const T &t) throw() {
75 if( !buf ) return *this;
86 const char* message() throw() {
91 mes = buf->str().c_str();
100 * \brief Generic exception class.
102 * Base class for exceptions used in LEMON.
104 class Exception : public std::exception {
109 virtual ~Exception() throw() {}
112 virtual const char* exceptionName() const {
113 return "lemon::Exception";
117 virtual const char* what() const throw() {
118 return exceptionName();
123 * \brief One of the two main subclasses of \ref Exception.
125 * Logic errors represent problems in the internal logic of a program;
126 * in theory, these are preventable, and even detectable before the
127 * program runs (e.g., violations of class invariants).
129 * For a typical example \see UninitializedParameterError.
131 class LogicError : public Exception {
133 virtual const char* exceptionName() const {
134 return "lemon::LogicError";
140 * \brief One of the two main subclasses of \ref Exception.
142 * Runtime errors represent problems outside the scope of a program;
143 * they cannot be easily predicted and can generally only be caught as
144 * the program executes.
146 class RuntimeError : public Exception {
148 virtual const char* exceptionName() const {
149 return "lemon::RuntimeError";
154 class RangeError : public RuntimeError {
156 virtual const char* exceptionName() const {
157 return "lemon::RangeError";
162 class IOError : public RuntimeError {
164 virtual const char* exceptionName() const {
165 return "lemon::IOError";
170 class DataFormatError : public IOError {
172 const char *_message;
174 boost::shared_ptr<std::string> _file;
178 explicit DataFormatError(const char *the_message)
179 : _message(the_message), _line(0) {}
181 DataFormatError(const std::string &file_name, int line_num,
182 const char *the_message)
183 : _message(the_message), _line(line_num) { file(file_name); }
186 void line(int line_num) { _line=line_num; }
188 void message(char *the_message) { _message=the_message; }
190 void file(const std::string &file_name) {
192 _file.reset(new std::string);
201 int line() const { return _line; }
203 const char* message() const { return _message; }
205 /// \brief Returns the filename.
207 /// Returns \e "(unknown)" if the filename was not specified.
208 const char* file() const {
210 return _file->c_str();
216 virtual const char* what() const throw() {
219 std::ostringstream ostr;
221 if( _file || _line ) {
223 if( _file ) ostr << "in file '" << *_file << "'";
224 if( _file && _line ) ostr << " ";
225 if( _line ) ostr << "at line " << _line;
228 mes = ostr.str().c_str();
231 if( mes ) return mes;
232 return exceptionName();
235 virtual const char* exceptionName() const {
236 return "lemon::DataFormatError";
239 virtual ~DataFormatError() throw() {}
244 class AssertionFailedError : public LogicError {
246 const char *assertion;
249 const char *function;
253 AssertionFailedError(const char *_file, int _line, const char *func,
254 const char *msg, const char *_assertion = 0) :
255 assertion(_assertion), file(_file), line(_line), function(func),
259 const char* get_assertion() const { return assertion; }
261 const char* get_message() const { return message; }
263 const char* get_file() const { return file; }
265 const char* get_function() const { return function; }
267 int get_line() const { return line; }
270 virtual const char* what() const throw() {
273 std::ostringstream ostr;
274 ostr << file << ":" << line << ": ";
276 ostr << function << ": ";
279 ostr << " (assertion '" << assertion << "' failed)";
280 mes = ostr.str().c_str();
283 if( mes ) return mes;
284 return exceptionName();
287 virtual const char* exceptionName() const {
288 return "lemon::AssertionFailedError";
291 virtual ~AssertionFailedError() throw() {}
295 /**************** Macros ****************/
299 void assert_fail(const char *file, int line, const char *func,
300 const char *message, const char *assertion = 0,
304 cerr << file << ":" << line << ": ";
306 cerr << func << ": ";
309 cerr << " (assertion '" << assertion << "' failed)";
316 void assert_fail_throw(const char *file, int line, const char *func,
317 const char *message, const char *assertion = 0,
320 throw AssertionFailedError(file, line, func, message, assertion);
325 #endif // LEMON_ERROR_H
330 #ifndef LEMON_ASSERT_ABORT
331 # define LEMON_ASSERT_ABORT 1
334 #ifndef LEMON_ASSERT_HANDLER
335 # ifdef LEMON_ASSERT_EXCEPTION
336 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
338 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail
342 #if defined(NDEBUG) || defined(LEMON_DISABLE_ASSERTS)
344 # define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
349 * \brief Macro for assertions with customizable message
351 * Macro for assertions with customizable message.
353 * The behaviour can be customized with LEMON_ASSERT_HANDLER,
354 * LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be
355 * disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros.
357 * \todo We should provide some way to reset to the default behaviour,
360 * \todo This whole 'assert' business should be placed in a separate
363 * \todo __PRETTY_FUNCTION__ should be replaced by something
364 * compiler-independant, like BOOST_CURRENT_FUNCTION
367 # define LEMON_ASSERT(exp, msg) \
368 (static_cast<void> (!!(exp) ? 0 : ( \
369 LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
370 __PRETTY_FUNCTION__, \
371 (msg), #exp, LEMON_ASSERT_ABORT), 0)))
373 #endif // NDEBUG || LEMON_DISABLE_ASSERTS
376 * \brief Macro for mark not yet implemented features.
378 * \todo Is this the right place for this? It should be used only in
379 * modules under development.
381 * \todo __PRETTY_FUNCTION__ should be replaced by something
382 * compiler-independant, like BOOST_CURRENT_FUNCTION
385 # define LEMON_FIXME(msg) \
386 (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \