lemon/error.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 11 Feb 2010 10:02:11 +0100
changeset 713 f58e01094738
parent 290 f6899946c1ac
child 440 88ed40ad0d4f
permissions -rw-r--r--
Fix the date field comment of graphToEps() output (#336)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     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.
    12  *
    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
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_ERROR_H
    20 #define LEMON_ERROR_H
    21 
    22 /// \ingroup exceptions
    23 /// \file
    24 /// \brief Basic exception classes and error handling.
    25 
    26 #include <exception>
    27 #include <string>
    28 #include <sstream>
    29 #include <iostream>
    30 #include <cstdlib>
    31 #include <memory>
    32 
    33 namespace lemon {
    34 
    35   /// \addtogroup exceptions
    36   /// @{
    37 
    38   /// \brief Generic exception class.
    39   ///
    40   /// Base class for exceptions used in LEMON.
    41   ///
    42   class Exception : public std::exception {
    43   public:
    44     ///Constructor
    45     Exception() throw() {}
    46     ///Virtual destructor
    47     virtual ~Exception() throw() {}
    48     ///A short description of the exception
    49     virtual const char* what() const throw() {
    50       return "lemon::Exception";
    51     }
    52   };
    53 
    54   /// \brief Input-Output error
    55   ///
    56   /// This exception is thrown when a file operation cannot be
    57   /// succeeded.
    58   class IoError : public Exception {
    59   protected:
    60     std::string _message;
    61     std::string _file;
    62 
    63     mutable std::string _what;
    64   public:
    65 
    66     /// Copy constructor
    67     IoError(const IoError &error) throw() : Exception() {
    68       message(error._message);
    69       file(error._file);
    70     }
    71 
    72     /// Constructor
    73     explicit IoError(const char *message) throw() {
    74       IoError::message(message);
    75     }
    76 
    77     /// Constructor
    78     explicit IoError(const std::string &message) throw() {
    79       IoError::message(message);
    80     }
    81 
    82     /// Constructor
    83     explicit IoError(const char *message,
    84                      const std::string &file) throw() {
    85       IoError::message(message);
    86       IoError::file(file);
    87     }
    88 
    89     /// Constructor
    90     explicit IoError(const std::string &message,
    91                      const std::string &file) throw() {
    92       IoError::message(message);
    93       IoError::file(file);
    94     }
    95 
    96     /// Virtual destructor
    97     virtual ~IoError() throw() {}
    98 
    99     /// Set the error message
   100     void message(const char *message) throw() {
   101       try {
   102         _message = message;
   103       } catch (...) {}
   104     }
   105 
   106     /// Set the error message
   107     void message(const std::string& message) throw() {
   108       try {
   109         _message = message;
   110       } catch (...) {}
   111     }
   112 
   113     /// Set the file name
   114     void file(const std::string &file) throw() {
   115       try {
   116         _file = file;
   117       } catch (...) {}
   118     }
   119 
   120     /// Returns the error message
   121     const std::string& message() const throw() {
   122       return _message;
   123     }
   124 
   125     /// \brief Returns the filename
   126     ///
   127     /// Returns the filename or an empty string if it was not specified.
   128     const std::string& file() const throw() {
   129       return _file;
   130     }
   131 
   132     /// \brief Returns a short error message
   133     ///
   134     /// Returns a short error message which contains the message and the
   135     /// file name.
   136     virtual const char* what() const throw() {
   137       try {
   138         _what.clear();
   139         std::ostringstream oss;
   140         oss << "lemon:IoError" << ": ";
   141         oss << _message;
   142         if (!_file.empty()) {
   143           oss << " ('" << _file << "')";
   144         }
   145         _what = oss.str();
   146       }
   147       catch (...) {}
   148       if (!_what.empty()) return _what.c_str();
   149       else return "lemon:IoError";
   150     }
   151 
   152   };
   153 
   154   /// \brief Format error
   155   ///
   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 {
   159   protected:
   160     std::string _message;
   161     std::string _file;
   162     int _line;
   163 
   164     mutable std::string _what;
   165   public:
   166 
   167     /// Copy constructor
   168     FormatError(const FormatError &error) throw() : Exception() {
   169       message(error._message);
   170       file(error._file);
   171       line(error._line);
   172     }
   173 
   174     /// Constructor
   175     explicit FormatError(const char *message) throw() {
   176       FormatError::message(message);
   177       _line = 0;
   178     }
   179 
   180     /// Constructor
   181     explicit FormatError(const std::string &message) throw() {
   182       FormatError::message(message);
   183       _line = 0;
   184     }
   185 
   186     /// Constructor
   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);
   192     }
   193 
   194     /// Constructor
   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);
   200     }
   201 
   202     /// Virtual destructor
   203     virtual ~FormatError() throw() {}
   204 
   205     /// Set the line number
   206     void line(int line) throw() { _line = line; }
   207 
   208     /// Set the error message
   209     void message(const char *message) throw() {
   210       try {
   211         _message = message;
   212       } catch (...) {}
   213     }
   214 
   215     /// Set the error message
   216     void message(const std::string& message) throw() {
   217       try {
   218         _message = message;
   219       } catch (...) {}
   220     }
   221 
   222     /// Set the file name
   223     void file(const std::string &file) throw() {
   224       try {
   225         _file = file;
   226       } catch (...) {}
   227     }
   228 
   229     /// \brief Returns the line number
   230     ///
   231     /// Returns the line number or zero if it was not specified.
   232     int line() const throw() { return _line; }
   233 
   234     /// Returns the error message
   235     const std::string& message() const throw() {
   236       return _message;
   237     }
   238 
   239     /// \brief Returns the filename
   240     ///
   241     /// Returns the filename or an empty string if it was not specified.
   242     const std::string& file() const throw() {
   243       return _file;
   244     }
   245 
   246     /// \brief Returns a short error message
   247     ///
   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() {
   251       try {
   252         _what.clear();
   253         std::ostringstream oss;
   254         oss << "lemon:FormatError" << ": ";
   255         oss << _message;
   256         if (!_file.empty() || _line != 0) {
   257           oss << " (";
   258           if (!_file.empty()) oss << "in file '" << _file << "'";
   259           if (!_file.empty() && _line != 0) oss << " ";
   260           if (_line != 0) oss << "at line " << _line;
   261           oss << ")";
   262         }
   263         _what = oss.str();
   264       }
   265       catch (...) {}
   266       if (!_what.empty()) return _what.c_str();
   267       else return "lemon:FormatError";
   268     }
   269 
   270   };
   271 
   272   /// @}
   273 
   274 }
   275 
   276 #endif // LEMON_ERROR_H