1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     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 Generic exception class.
 
    40   /// Base class for exceptions used in LEMON.
 
    42   class Exception : public std::exception {
 
    45     Exception() throw() {}
 
    47     virtual ~Exception() throw() {}
 
    48     ///A short description of the exception
 
    49     virtual const char* what() const throw() {
 
    50       return "lemon::Exception";
 
    54   /// \brief Input-Output error
 
    56   /// This exception is thrown when a file operation cannot be
 
    58   class IoError : public Exception {
 
    63     mutable std::string _what;
 
    67     IoError(const IoError &error) throw() : Exception() {
 
    68       message(error._message);
 
    73     explicit IoError(const char *message) throw() {
 
    74       IoError::message(message);
 
    78     explicit IoError(const std::string &message) throw() {
 
    79       IoError::message(message);
 
    83     explicit IoError(const char *message,
 
    84                      const std::string &file) throw() {
 
    85       IoError::message(message);
 
    90     explicit IoError(const std::string &message,
 
    91                      const std::string &file) throw() {
 
    92       IoError::message(message);
 
    96     /// Virtual destructor
 
    97     virtual ~IoError() throw() {}
 
    99     /// Set the error message
 
   100     void message(const char *message) throw() {
 
   106     /// Set the error message
 
   107     void message(const std::string& message) throw() {
 
   113     /// Set the file name
 
   114     void file(const std::string &file) throw() {
 
   120     /// Returns the error message
 
   121     const std::string& message() const throw() {
 
   125     /// \brief Returns the filename
 
   127     /// Returns the filename or an empty string if it was not specified.
 
   128     const std::string& file() const throw() {
 
   132     /// \brief Returns a short error message
 
   134     /// Returns a short error message which contains the message and the
 
   136     virtual const char* what() const throw() {
 
   139         std::ostringstream oss;
 
   140         oss << "lemon:IoError" << ": ";
 
   142         if (!_file.empty()) {
 
   143           oss << " ('" << _file << "')";
 
   148       if (!_what.empty()) return _what.c_str();
 
   149       else return "lemon:IoError";
 
   154   /// \brief Format error
 
   156   /// This exception is thrown when an input file has wrong
 
   157   /// format or a data representation is not legal.
 
   158   class FormatError : public Exception {
 
   160     std::string _message;
 
   164     mutable std::string _what;
 
   168     FormatError(const FormatError &error) throw() : Exception() {
 
   169       message(error._message);
 
   175     explicit FormatError(const char *message) throw() {
 
   176       FormatError::message(message);
 
   181     explicit FormatError(const std::string &message) throw() {
 
   182       FormatError::message(message);
 
   187     explicit FormatError(const char *message,
 
   188                          const std::string &file, int line = 0) throw() {
 
   189       FormatError::message(message);
 
   190       FormatError::file(file);
 
   191       FormatError::line(line);
 
   195     explicit FormatError(const std::string &message,
 
   196                          const std::string &file, int line = 0) throw() {
 
   197       FormatError::message(message);
 
   198       FormatError::file(file);
 
   199       FormatError::line(line);
 
   202     /// Virtual destructor
 
   203     virtual ~FormatError() throw() {}
 
   205     /// Set the line number
 
   206     void line(int line) throw() { _line = line; }
 
   208     /// Set the error message
 
   209     void message(const char *message) throw() {
 
   215     /// Set the error message
 
   216     void message(const std::string& message) throw() {
 
   222     /// Set the file name
 
   223     void file(const std::string &file) throw() {
 
   229     /// \brief Returns the line number
 
   231     /// Returns the line number or zero if it was not specified.
 
   232     int line() const throw() { return _line; }
 
   234     /// Returns the error message
 
   235     const std::string& message() const throw() {
 
   239     /// \brief Returns the filename
 
   241     /// Returns the filename or an empty string if it was not specified.
 
   242     const std::string& file() const throw() {
 
   246     /// \brief Returns a short error message
 
   248     /// Returns a short error message which contains the message, the
 
   249     /// file name and the line number.
 
   250     virtual const char* what() const throw() {
 
   253         std::ostringstream oss;
 
   254         oss << "lemon:FormatError" << ": ";
 
   256         if (!_file.empty() || _line != 0) {
 
   258           if (!_file.empty()) oss << "in file '" << _file << "'";
 
   259           if (!_file.empty() && _line != 0) oss << " ";
 
   260           if (_line != 0) oss << "at line " << _line;
 
   266       if (!_what.empty()) return _what.c_str();
 
   267       else return "lemon:FormatError";
 
   276 #endif // LEMON_ERROR_H