COIN-OR::LEMON - Graph Library

source: lemon-1.2/lemon/error.h @ 340:0badf3bb38c2

Last change on this file since 340:0badf3bb38c2 was 291:d901321d6555, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Changing parameter order in exception classes + improvements

File size: 6.7 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[66]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[66]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
33namespace lemon {
34
35  /// \addtogroup exceptions
36  /// @{
37
[290]38  /// \brief Generic exception class.
[66]39  ///
40  /// Base class for exceptions used in LEMON.
41  ///
42  class Exception : public std::exception {
43  public:
[291]44    ///Constructor
45    Exception() throw() {}
46    ///Virtual destructor
[66]47    virtual ~Exception() throw() {}
[291]48    ///A short description of the exception
[66]49    virtual const char* what() const throw() {
50      return "lemon::Exception";
51    }
52  };
53
[290]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;
[66]62
[290]63    mutable std::string _what;
[66]64  public:
[290]65
66    /// Copy constructor
[291]67    IoError(const IoError &error) throw() : Exception() {
[290]68      message(error._message);
69      file(error._file);
70    }
71
72    /// Constructor
[291]73    explicit IoError(const char *message) throw() {
[290]74      IoError::message(message);
75    }
76
77    /// Constructor
[291]78    explicit IoError(const std::string &message) throw() {
[290]79      IoError::message(message);
80    }
81
82    /// Constructor
[291]83    explicit IoError(const char *message,
84                     const std::string &file) throw() {
[290]85      IoError::message(message);
86      IoError::file(file);
87    }
88
89    /// Constructor
[291]90    explicit IoError(const std::string &message,
91                     const std::string &file) throw() {
[290]92      IoError::message(message);
93      IoError::file(file);
94    }
95
96    /// Virtual destructor
97    virtual ~IoError() throw() {}
98
99    /// Set the error message
[291]100    void message(const char *message) throw() {
[290]101      try {
102        _message = message;
103      } catch (...) {}
104    }
105
106    /// Set the error message
[291]107    void message(const std::string& message) throw() {
[290]108      try {
109        _message = message;
110      } catch (...) {}
111    }
112
113    /// Set the file name
[291]114    void file(const std::string &file) throw() {
[290]115      try {
116        _file = file;
117      } catch (...) {}
118    }
119
120    /// Returns the error message
[291]121    const std::string& message() const throw() {
[290]122      return _message;
123    }
124
125    /// \brief Returns the filename
126    ///
[291]127    /// Returns the filename or an empty string if it was not specified.
128    const std::string& file() const throw() {
[290]129      return _file;
130    }
131
132    /// \brief Returns a short error message
133    ///
[291]134    /// Returns a short error message which contains the message and the
135    /// file name.
[66]136    virtual const char* what() const throw() {
[290]137      try {
138        _what.clear();
139        std::ostringstream oss;
140        oss << "lemon:IoError" << ": ";
[291]141        oss << _message;
142        if (!_file.empty()) {
143          oss << " ('" << _file << "')";
[290]144        }
145        _what = oss.str();
146      }
147      catch (...) {}
148      if (!_what.empty()) return _what.c_str();
149      else return "lemon:IoError";
[66]150    }
[290]151
[66]152  };
153
[290]154  /// \brief Format error
155  ///
[291]156  /// This exception is thrown when an input file has wrong
157  /// format or a data representation is not legal.
[290]158  class FormatError : public Exception {
[66]159  protected:
[290]160    std::string _message;
161    std::string _file;
[66]162    int _line;
163
[290]164    mutable std::string _what;
[66]165  public:
166
[290]167    /// Copy constructor
[291]168    FormatError(const FormatError &error) throw() : Exception() {
[290]169      message(error._message);
170      file(error._file);
171      line(error._line);
[66]172    }
173
[290]174    /// Constructor
[291]175    explicit FormatError(const char *message) throw() {
[290]176      FormatError::message(message);
177      _line = 0;
[66]178    }
179
[290]180    /// Constructor
[291]181    explicit FormatError(const std::string &message) throw() {
[290]182      FormatError::message(message);
183      _line = 0;
184    }
185
186    /// Constructor
[291]187    explicit FormatError(const char *message,
188                         const std::string &file, int line = 0) throw() {
[290]189      FormatError::message(message);
190      FormatError::file(file);
191      FormatError::line(line);
192    }
193
194    /// Constructor
[291]195    explicit FormatError(const std::string &message,
196                         const std::string &file, int line = 0) throw() {
[290]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
[291]206    void line(int line) throw() { _line = line; }
[290]207
208    /// Set the error message
[291]209    void message(const char *message) throw() {
[290]210      try {
211        _message = message;
212      } catch (...) {}
213    }
214
215    /// Set the error message
[291]216    void message(const std::string& message) throw() {
[290]217      try {
218        _message = message;
219      } catch (...) {}
220    }
221
222    /// Set the file name
[291]223    void file(const std::string &file) throw() {
[290]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.
[291]232    int line() const throw() { return _line; }
[290]233
234    /// Returns the error message
[291]235    const std::string& message() const throw() {
[290]236      return _message;
237    }
238
239    /// \brief Returns the filename
240    ///
[291]241    /// Returns the filename or an empty string if it was not specified.
242    const std::string& file() const throw() {
[290]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.
[66]250    virtual const char* what() const throw() {
251      try {
[290]252        _what.clear();
253        std::ostringstream oss;
254        oss << "lemon:FormatError" << ": ";
[291]255        oss << _message;
256        if (!_file.empty() || _line != 0) {
[290]257          oss << " (";
[291]258          if (!_file.empty()) oss << "in file '" << _file << "'";
259          if (!_file.empty() && _line != 0) oss << " ";
260          if (_line != 0) oss << "at line " << _line;
[290]261          oss << ")";
[209]262        }
[290]263        _what = oss.str();
[66]264      }
265      catch (...) {}
[290]266      if (!_what.empty()) return _what.c_str();
267      else return "lemon:FormatError";
[66]268    }
269
270  };
271
[108]272  /// @}
[66]273
274}
[108]275
[66]276#endif // LEMON_ERROR_H
Note: See TracBrowser for help on using the repository browser.