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 error handling (signaling) routines.
28 #include <boost/shared_ptr.hpp>
32 /// Exception-safe convenient "error message" class.
35 boost::shared_ptr<std::ostringstream> buf;
39 buf.reset(new std::ostringstream);
49 ErrorMessage() throw() { init(); }
51 ErrorMessage(const char *message) throw() {
56 ErrorMessage(const std::string &message) throw() {
62 ErrorMessage& operator<<(const T &t) throw() {
63 if( !buf ) return *this;
73 const char* message() throw() {
78 mes = buf->str().c_str();
87 * \brief Generic exception class.
89 * Base class for exceptions used in LEMON.
91 class Exception : public std::exception, public ErrorMessage {
93 Exception() throw() {}
94 explicit Exception(const std::string &s) throw()
96 virtual ~Exception() throw() {}
98 virtual const char* what() const throw() {
99 const char *mes = message();
100 if( mes ) return mes;
101 return "lemon::Exception";
106 class LogicError : public Exception {
107 explicit LogicError(const std::string &s)
111 class RuntimeError : public Exception {
112 explicit RuntimeError(const std::string &s)
116 class RangeError : public RuntimeError {
117 explicit RangeError(const std::string &s)
121 class IOError : public RuntimeError {
122 explicit IOError(const std::string &s)
126 class DataFormatError : public IOError {
127 explicit DataFormatError(const std::string &message)
128 : IOError(message) : line(0) {}
129 DataFormatError(const std::string &file_name, int line_num,
130 sconst std::string &message)
131 : IOError(message), line(line_num) { set_file(file_name); }
133 void set_line(int line_num) { line=line_num; }
134 void set_file(const std::string &file_name) {
136 file.reset(new std::string);
144 virtual const char* what() const {
147 std::ostringstream ostr;
148 ostr << IOError::what();
151 if( file ) ostr << "in file" << *file;
152 if( line ) ostr << " at line" << line;
154 mes = ostr.str().c_str();
157 if( mes ) return mes;
158 return "lemon::DataFormatError";
164 /**************** Macros ****************/
168 * \brief Macro for assertions with customizable message
171 # define lemon_assert(exp, msg) \
173 std::cerr << __FILE__ ":" << __LINE__ << ": " << (msg) << std::endl; \
179 * \brief Macro for mark not yet implemented features.
181 * \todo Is this the right place for this? It should be used only in
182 * modules under development.
185 # define FIXME(msg) lemon_assert(0, "FIXME: " msg)
188 #endif // LEMON_ERROR_H