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.
36 boost::shared_ptr<std::ostringstream> buf;
41 buf.reset(new std::ostringstream);
52 ErrorMessage() throw() { init(); }
55 ErrorMessage(const char *message) throw() {
61 ErrorMessage(const std::string &message) throw() {
68 ErrorMessage& operator<<(const T &t) throw() {
69 if( !buf ) return *this;
80 const char* message() throw() {
85 mes = buf->str().c_str();
94 * \brief Generic exception class.
96 * Base class for exceptions used in LEMON.
98 class Exception : public std::exception, public ErrorMessage {
101 Exception() throw() {}
103 explicit Exception(const std::string &s) throw()
106 virtual ~Exception() throw() {}
109 virtual const char* what() const throw() {
110 const char *mes = message();
111 if( mes ) return mes;
112 return "lemon::Exception";
117 class LogicError : public Exception {
119 explicit LogicError(const std::string &s)
124 class RuntimeError : public Exception {
126 explicit RuntimeError(const std::string &s)
131 class RangeError : public RuntimeError {
133 explicit RangeError(const std::string &s)
138 class IOError : public RuntimeError {
140 explicit IOError(const std::string &s)
145 class DataFormatError : public IOError {
147 explicit DataFormatError(const std::string &message)
148 : IOError(message) : line(0) {}
150 DataFormatError(const std::string &file_name, int line_num,
151 const std::string &message)
152 : IOError(message), line(line_num) { set_file(file_name); }
155 void set_line(int line_num) { line=line_num; }
157 void set_file(const std::string &file_name) {
159 file.reset(new std::string);
168 virtual const char* what() const {
171 std::ostringstream ostr;
172 ostr << IOError::what();
175 if( file ) ostr << "in file" << *file;
176 if( line ) ostr << " at line" << line;
178 mes = ostr.str().c_str();
181 if( mes ) return mes;
182 return "lemon::DataFormatError";
188 /**************** Macros ****************/
192 * \brief Macro for assertions with customizable message
195 # define lemon_assert(exp, msg) \
197 std::cerr << __FILE__ ":" << __LINE__ << ": " << (msg) << std::endl; \
203 * \brief Macro for mark not yet implemented features.
205 * \todo Is this the right place for this? It should be used only in
206 * modules under development.
209 # define FIXME(msg) lemon_assert(0, "FIXME: " msg)
212 #endif // LEMON_ERROR_H