alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@66: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. kpeter@66: * kpeter@66: * Copyright (C) 2003-2008 kpeter@66: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@66: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@66: * kpeter@66: * Permission to use, modify and distribute this software is granted kpeter@66: * provided that this copyright notice appears in all copies. For kpeter@66: * precise terms see the accompanying LICENSE file. kpeter@66: * kpeter@66: * This software is provided "AS IS" with no warranty of any kind, kpeter@66: * express or implied, and with no claim as to its suitability for any kpeter@66: * purpose. kpeter@66: * kpeter@66: */ kpeter@66: kpeter@66: #ifndef LEMON_ERROR_H kpeter@66: #define LEMON_ERROR_H kpeter@66: kpeter@66: /// \ingroup exceptions kpeter@66: /// \file kpeter@66: /// \brief Basic exception classes and error handling. kpeter@66: kpeter@66: #include kpeter@66: #include kpeter@66: #include kpeter@66: #include kpeter@66: #include kpeter@66: #include kpeter@66: kpeter@66: namespace lemon { kpeter@66: kpeter@66: /// \addtogroup exceptions kpeter@66: /// @{ kpeter@66: deba@290: /// \brief Generic exception class. kpeter@66: /// kpeter@66: /// Base class for exceptions used in LEMON. kpeter@66: /// kpeter@66: class Exception : public std::exception { kpeter@66: public: kpeter@291: ///Constructor kpeter@291: Exception() throw() {} kpeter@291: ///Virtual destructor kpeter@66: virtual ~Exception() throw() {} kpeter@291: ///A short description of the exception kpeter@66: virtual const char* what() const throw() { kpeter@66: return "lemon::Exception"; kpeter@66: } kpeter@66: }; kpeter@66: deba@290: /// \brief Input-Output error deba@290: /// deba@290: /// This exception is thrown when a file operation cannot be deba@290: /// succeeded. deba@290: class IoError : public Exception { deba@290: protected: deba@290: std::string _message; deba@290: std::string _file; kpeter@66: deba@290: mutable std::string _what; kpeter@66: public: deba@290: deba@290: /// Copy constructor kpeter@291: IoError(const IoError &error) throw() : Exception() { deba@290: message(error._message); deba@290: file(error._file); deba@290: } deba@290: deba@290: /// Constructor kpeter@291: explicit IoError(const char *message) throw() { deba@290: IoError::message(message); deba@290: } deba@290: deba@290: /// Constructor kpeter@291: explicit IoError(const std::string &message) throw() { deba@290: IoError::message(message); deba@290: } deba@290: deba@290: /// Constructor kpeter@291: explicit IoError(const char *message, kpeter@291: const std::string &file) throw() { deba@290: IoError::message(message); deba@290: IoError::file(file); deba@290: } deba@290: deba@290: /// Constructor kpeter@291: explicit IoError(const std::string &message, kpeter@291: const std::string &file) throw() { deba@290: IoError::message(message); deba@290: IoError::file(file); deba@290: } deba@290: deba@290: /// Virtual destructor deba@290: virtual ~IoError() throw() {} deba@290: deba@290: /// Set the error message kpeter@291: void message(const char *message) throw() { deba@290: try { deba@290: _message = message; deba@290: } catch (...) {} deba@290: } deba@290: deba@290: /// Set the error message kpeter@291: void message(const std::string& message) throw() { deba@290: try { deba@290: _message = message; deba@290: } catch (...) {} deba@290: } deba@290: deba@290: /// Set the file name kpeter@291: void file(const std::string &file) throw() { deba@290: try { deba@290: _file = file; deba@290: } catch (...) {} deba@290: } deba@290: deba@290: /// Returns the error message kpeter@291: const std::string& message() const throw() { deba@290: return _message; deba@290: } deba@290: deba@290: /// \brief Returns the filename deba@290: /// kpeter@291: /// Returns the filename or an empty string if it was not specified. kpeter@291: const std::string& file() const throw() { deba@290: return _file; deba@290: } deba@290: deba@290: /// \brief Returns a short error message deba@290: /// kpeter@291: /// Returns a short error message which contains the message and the kpeter@291: /// file name. kpeter@66: virtual const char* what() const throw() { deba@290: try { deba@290: _what.clear(); deba@290: std::ostringstream oss; deba@290: oss << "lemon:IoError" << ": "; kpeter@291: oss << _message; kpeter@291: if (!_file.empty()) { kpeter@291: oss << " ('" << _file << "')"; deba@290: } deba@290: _what = oss.str(); deba@290: } deba@290: catch (...) {} deba@290: if (!_what.empty()) return _what.c_str(); deba@290: else return "lemon:IoError"; kpeter@66: } deba@290: kpeter@66: }; kpeter@66: deba@290: /// \brief Format error deba@290: /// kpeter@291: /// This exception is thrown when an input file has wrong kpeter@291: /// format or a data representation is not legal. deba@290: class FormatError : public Exception { kpeter@66: protected: deba@290: std::string _message; deba@290: std::string _file; kpeter@66: int _line; kpeter@66: deba@290: mutable std::string _what; kpeter@66: public: kpeter@66: deba@290: /// Copy constructor kpeter@291: FormatError(const FormatError &error) throw() : Exception() { deba@290: message(error._message); deba@290: file(error._file); deba@290: line(error._line); kpeter@66: } kpeter@66: deba@290: /// Constructor kpeter@291: explicit FormatError(const char *message) throw() { deba@290: FormatError::message(message); deba@290: _line = 0; kpeter@66: } kpeter@66: deba@290: /// Constructor kpeter@291: explicit FormatError(const std::string &message) throw() { deba@290: FormatError::message(message); deba@290: _line = 0; deba@290: } deba@290: deba@290: /// Constructor kpeter@291: explicit FormatError(const char *message, kpeter@291: const std::string &file, int line = 0) throw() { deba@290: FormatError::message(message); deba@290: FormatError::file(file); deba@290: FormatError::line(line); deba@290: } deba@290: deba@290: /// Constructor kpeter@291: explicit FormatError(const std::string &message, kpeter@291: const std::string &file, int line = 0) throw() { deba@290: FormatError::message(message); deba@290: FormatError::file(file); deba@290: FormatError::line(line); deba@290: } deba@290: deba@290: /// Virtual destructor deba@290: virtual ~FormatError() throw() {} deba@290: deba@290: /// Set the line number kpeter@291: void line(int line) throw() { _line = line; } deba@290: deba@290: /// Set the error message kpeter@291: void message(const char *message) throw() { deba@290: try { deba@290: _message = message; deba@290: } catch (...) {} deba@290: } deba@290: deba@290: /// Set the error message kpeter@291: void message(const std::string& message) throw() { deba@290: try { deba@290: _message = message; deba@290: } catch (...) {} deba@290: } deba@290: deba@290: /// Set the file name kpeter@291: void file(const std::string &file) throw() { deba@290: try { deba@290: _file = file; deba@290: } catch (...) {} deba@290: } deba@290: deba@290: /// \brief Returns the line number deba@290: /// deba@290: /// Returns the line number or zero if it was not specified. kpeter@291: int line() const throw() { return _line; } deba@290: deba@290: /// Returns the error message kpeter@291: const std::string& message() const throw() { deba@290: return _message; deba@290: } deba@290: deba@290: /// \brief Returns the filename deba@290: /// kpeter@291: /// Returns the filename or an empty string if it was not specified. kpeter@291: const std::string& file() const throw() { deba@290: return _file; deba@290: } deba@290: deba@290: /// \brief Returns a short error message deba@290: /// deba@290: /// Returns a short error message which contains the message, the deba@290: /// file name and the line number. kpeter@66: virtual const char* what() const throw() { kpeter@66: try { deba@290: _what.clear(); deba@290: std::ostringstream oss; deba@290: oss << "lemon:FormatError" << ": "; kpeter@291: oss << _message; kpeter@291: if (!_file.empty() || _line != 0) { deba@290: oss << " ("; kpeter@291: if (!_file.empty()) oss << "in file '" << _file << "'"; kpeter@291: if (!_file.empty() && _line != 0) oss << " "; kpeter@291: if (_line != 0) oss << "at line " << _line; deba@290: oss << ")"; alpar@209: } deba@290: _what = oss.str(); kpeter@66: } kpeter@66: catch (...) {} deba@290: if (!_what.empty()) return _what.c_str(); deba@290: else return "lemon:FormatError"; kpeter@66: } kpeter@66: kpeter@66: }; kpeter@66: deba@108: /// @} kpeter@66: kpeter@66: } deba@108: kpeter@66: #endif // LEMON_ERROR_H