1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
22 /// \ingroup exceptions
24 /// \brief Basic exception classes and error handling.
35 /// \addtogroup exceptions
38 /// \brief Exception safe wrapper class.
40 /// Exception safe wrapper class to implement the members of exceptions.
41 template <typename _Type>
42 class ExceptionMember {
46 ExceptionMember() throw() {
48 ptr.reset(new Type());
52 ExceptionMember(const Type& type) throw() {
54 ptr.reset(new Type());
55 if (ptr.get() == 0) return;
60 ExceptionMember(const ExceptionMember& copy) throw() {
62 if (!copy.valid()) return;
63 ptr.reset(new Type());
64 if (ptr.get() == 0) return;
69 ExceptionMember& operator=(const ExceptionMember& copy) throw() {
70 if (ptr.get() == 0) return;
72 if (!copy.valid()) return;
77 void set(const Type& type) throw() {
78 if (ptr.get() == 0) return;
84 const Type& get() const {
88 bool valid() const throw() {
89 return ptr.get() != 0;
93 std::auto_ptr<_Type> ptr;
96 /// Exception-safe convenient error message builder class.
98 /// Helper class which provides a convenient ostream-like (operator <<
99 /// based) interface to create a string message. Mostly useful in
100 /// exception classes (therefore the name).
105 mutable std::auto_ptr<std::ostringstream> buf;
108 bool init() throw() {
110 buf.reset(new std::ostringstream);
121 ErrorMessage() throw() { init(); }
123 ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
126 ErrorMessage(const char *msg) throw() {
132 ErrorMessage(const std::string &msg) throw() {
138 template <typename T>
139 ErrorMessage& operator<<(const T &t) throw() {
140 if( ! buf.get() ) return *this;
152 const char* message() throw() {
153 if( ! buf.get() ) return 0;
157 mes = buf->str().c_str();
165 /// Generic exception class.
167 /// Base class for exceptions used in LEMON.
169 class Exception : public std::exception {
174 virtual ~Exception() throw() {}
176 virtual const char* what() const throw() {
177 return "lemon::Exception";
181 /// One of the two main subclasses of \ref Exception.
183 /// Logic errors represent problems in the internal logic of a program;
184 /// in theory, these are preventable, and even detectable before the
185 /// program runs (e.g. violations of class invariants).
187 /// A typical example for this is \ref UninitializedParameter.
188 class LogicError : public Exception {
190 virtual const char* what() const throw() {
191 return "lemon::LogicError";
195 /// \ref Exception for uninitialized parameters.
197 /// This error represents problems in the initialization
198 /// of the parameters of the algorithms.
199 class UninitializedParameter : public LogicError {
201 virtual const char* what() const throw() {
202 return "lemon::UninitializedParameter";
207 /// One of the two main subclasses of \ref Exception.
209 /// Runtime errors represent problems outside the scope of a program;
210 /// they cannot be easily predicted and can generally only be caught
211 /// as the program executes.
212 class RuntimeError : public Exception {
214 virtual const char* what() const throw() {
215 return "lemon::RuntimeError";
220 class RangeError : public RuntimeError {
222 virtual const char* what() const throw() {
223 return "lemon::RangeError";
228 class IoError : public RuntimeError {
230 virtual const char* what() const throw() {
231 return "lemon::IoError";
236 class DataFormatError : public IoError {
238 ExceptionMember<std::string> _message;
239 ExceptionMember<std::string> _file;
242 mutable ExceptionMember<std::string> _message_holder;
245 DataFormatError(const DataFormatError &dfe) :
246 IoError(dfe), _message(dfe._message), _file(dfe._file),
250 explicit DataFormatError(const char *the_message)
251 : _message(the_message), _line(0) {}
254 DataFormatError(const std::string &file_name, int line_num,
255 const char *the_message)
256 : _message(the_message), _line(line_num) { file(file_name); }
259 void line(int ln) { _line = ln; }
261 void message(const std::string& msg) { _message.set(msg); }
263 void file(const std::string &fl) { _file.set(fl); }
266 int line() const { return _line; }
268 const char* message() const {
269 if (_message.valid() && !_message.get().empty()) {
270 return _message.get().c_str();
276 /// \brief Returns the filename.
278 /// Returns \e null if the filename was not specified.
279 const char* file() const {
280 if (_file.valid() && !_file.get().empty()) {
281 return _file.get().c_str();
288 virtual const char* what() const throw() {
290 std::ostringstream ostr;
291 ostr << "lemon:DataFormatError" << ": ";
292 if (message()) ostr << message();
293 if( file() || line() != 0 ) {
295 if( file() ) ostr << "in file '" << file() << "'";
296 if( file() && line() != 0 ) ostr << " ";
297 if( line() != 0 ) ostr << "at line " << line();
300 _message_holder.set(ostr.str());
303 if( _message_holder.valid()) return _message_holder.get().c_str();
304 return "lemon:DataFormatError";
307 virtual ~DataFormatError() throw() {}
311 class FileOpenError : public IoError {
313 ExceptionMember<std::string> _file;
315 mutable ExceptionMember<std::string> _message_holder;
318 FileOpenError(const FileOpenError &foe) :
319 IoError(foe), _file(foe._file) {}
322 explicit FileOpenError(const std::string& fl)
327 void file(const std::string &fl) { _file.set(fl); }
329 /// \brief Returns the filename.
331 /// Returns \e null if the filename was not specified.
332 const char* file() const {
333 if (_file.valid() && !_file.get().empty()) {
334 return _file.get().c_str();
341 virtual const char* what() const throw() {
343 std::ostringstream ostr;
344 ostr << "lemon::FileOpenError" << ": ";
345 ostr << "Cannot open file - " << file();
346 _message_holder.set(ostr.str());
349 if( _message_holder.valid()) return _message_holder.get().c_str();
350 return "lemon::FileOpenError";
352 virtual ~FileOpenError() throw() {}
355 class IoParameterError : public IoError {
357 ExceptionMember<std::string> _message;
358 ExceptionMember<std::string> _file;
360 mutable ExceptionMember<std::string> _message_holder;
363 IoParameterError(const IoParameterError &ile) :
364 IoError(ile), _message(ile._message), _file(ile._file) {}
367 explicit IoParameterError(const char *the_message)
368 : _message(the_message) {}
371 IoParameterError(const char *file_name, const char *the_message)
372 : _message(the_message), _file(file_name) {}
375 void message(const std::string& msg) { _message.set(msg); }
377 void file(const std::string &fl) { _file.set(fl); }
380 const char* message() const {
381 if (_message.valid()) {
382 return _message.get().c_str();
388 /// \brief Returns the filename.
390 /// Returns \c 0 if the filename was not specified.
391 const char* file() const {
393 return _file.get().c_str();
400 virtual const char* what() const throw() {
402 std::ostringstream ostr;
403 if (message()) ostr << message();
404 if (file()) ostr << "(when reading file '" << file() << "')";
405 _message_holder.set(ostr.str());
408 if( _message_holder.valid() ) return _message_holder.get().c_str();
409 return "lemon:IoParameterError";
411 virtual ~IoParameterError() throw() {}
418 #endif // LEMON_ERROR_H