COIN-OR::LEMON - Graph Library

source: lemon/lemon/error.h @ 290:f6899946c1ac

Last change on this file since 290:f6899946c1ac was 290:f6899946c1ac, checked in by Balazs Dezso <deba@…>, 11 years ago

Simplifying exceptions

  • Using asserts instead of exceptions for unitialized parameters
  • Only the IO exceptions are used in the lemon
  • DataFormatError? is renamed to FormatError?
  • The IoError? is used for file access errors
File size: 6.5 KB
Line 
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
33namespace 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    ///\e Constructor
45    Exception() {}
46    ///\e Virtual destructor
47    virtual ~Exception() throw() {}
48    ///\e 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) {
68      message(error._message);
69      file(error._file);
70    }
71
72    /// Constructor
73    explicit IoError(const char *message) {
74      IoError::message(message);
75    }
76
77    /// Constructor
78    explicit IoError(const std::string &message) {
79      IoError::message(message);
80    }
81
82    /// Constructor
83    IoError(const std::string &file, const char *message) {
84      IoError::message(message);
85      IoError::file(file);
86    }
87
88    /// Constructor
89    IoError(const std::string &file, const std::string &message) {
90      IoError::message(message);
91      IoError::file(file);
92    }
93
94    /// Virtual destructor
95    virtual ~IoError() throw() {}
96
97    /// Set the error message
98    void message(const char *message) {
99      try {
100        _message = message;
101      } catch (...) {}
102    }
103
104    /// Set the error message
105    void message(const std::string& message) {
106      try {
107        _message = message;
108      } catch (...) {}
109    }
110
111    /// Set the file name
112    void file(const std::string &file) {
113      try {
114        _file = file;
115      } catch (...) {}
116    }
117
118    /// Returns the error message
119    const std::string& message() const {
120      return _message;
121    }
122
123    /// \brief Returns the filename
124    ///
125    /// Returns the filename or empty string if the filename was not
126    /// specified.
127    const std::string& file() const {
128      return _file;
129    }
130
131    /// \brief Returns a short error message
132    ///
133    /// Returns a short error message which contains the message, the
134    /// file name and the line number.
135    virtual const char* what() const throw() {
136      try {
137        _what.clear();
138        std::ostringstream oss;
139        oss << "lemon:IoError" << ": ";
140        oss << message();
141        if (!file().empty()) {
142          oss << " (";
143          if (!file().empty()) oss << "with file '" << file() << "'";
144          oss << ")";
145        }
146        _what = oss.str();
147      }
148      catch (...) {}
149      if (!_what.empty()) return _what.c_str();
150      else return "lemon:IoError";
151    }
152
153  };
154
155  /// \brief Format error
156  ///
157  /// This class is used to indicate if an input file has wrong
158  /// formatting, or a data representation is not legal.
159  class FormatError : public Exception {
160  protected:
161    std::string _message;
162    std::string _file;
163    int _line;
164
165    mutable std::string _what;
166  public:
167
168    /// Copy constructor
169    FormatError(const FormatError &error) {
170      message(error._message);
171      file(error._file);
172      line(error._line);
173    }
174
175    /// Constructor
176    explicit FormatError(const char *message) {
177      FormatError::message(message);
178      _line = 0;
179    }
180
181    /// Constructor
182    explicit FormatError(const std::string &message) {
183      FormatError::message(message);
184      _line = 0;
185    }
186
187    /// Constructor
188    FormatError(const std::string &file, int line, const char *message) {
189      FormatError::message(message);
190      FormatError::file(file);
191      FormatError::line(line);
192    }
193
194    /// Constructor
195    FormatError(const std::string &file, int line, const std::string &message) {
196      FormatError::message(message);
197      FormatError::file(file);
198      FormatError::line(line);
199    }
200
201    /// Virtual destructor
202    virtual ~FormatError() throw() {}
203
204    /// Set the line number
205    void line(int line) { _line = line; }
206
207    /// Set the error message
208    void message(const char *message) {
209      try {
210        _message = message;
211      } catch (...) {}
212    }
213
214    /// Set the error message
215    void message(const std::string& message) {
216      try {
217        _message = message;
218      } catch (...) {}
219    }
220
221    /// Set the file name
222    void file(const std::string &file) {
223      try {
224        _file = file;
225      } catch (...) {}
226    }
227
228    /// \brief Returns the line number
229    ///
230    /// Returns the line number or zero if it was not specified.
231    int line() const { return _line; }
232
233    /// Returns the error message
234    const std::string& message() const {
235      return _message;
236    }
237
238    /// \brief Returns the filename
239    ///
240    /// Returns the filename or empty string if the filename was not
241    /// specified.
242    const std::string& file() const {
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
Note: See TracBrowser for help on using the repository browser.