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