1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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 {
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";
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) {
68 message(error._message);
73 explicit IoError(const char *message) {
74 IoError::message(message);
78 explicit IoError(const std::string &message) {
79 IoError::message(message);
83 IoError(const std::string &file, const char *message) {
84 IoError::message(message);
89 IoError(const std::string &file, const std::string &message) {
90 IoError::message(message);
94 /// Virtual destructor
95 virtual ~IoError() throw() {}
97 /// Set the error message
98 void message(const char *message) {
104 /// Set the error message
105 void message(const std::string& message) {
111 /// Set the file name
112 void file(const std::string &file) {
118 /// Returns the error message
119 const std::string& message() const {
123 /// \brief Returns the filename
125 /// Returns the filename or empty string if the filename was not
127 const std::string& file() const {
131 /// \brief Returns a short error message
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() {
138 std::ostringstream oss;
139 oss << "lemon:IoError" << ": ";
141 if (!file().empty()) {
143 if (!file().empty()) oss << "with file '" << file() << "'";
149 if (!_what.empty()) return _what.c_str();
150 else return "lemon:IoError";
155 /// \brief Format error
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 {
161 std::string _message;
165 mutable std::string _what;
169 FormatError(const FormatError &error) {
170 message(error._message);
176 explicit FormatError(const char *message) {
177 FormatError::message(message);
182 explicit FormatError(const std::string &message) {
183 FormatError::message(message);
188 FormatError(const std::string &file, int line, const char *message) {
189 FormatError::message(message);
190 FormatError::file(file);
191 FormatError::line(line);
195 FormatError(const std::string &file, int line, const std::string &message) {
196 FormatError::message(message);
197 FormatError::file(file);
198 FormatError::line(line);
201 /// Virtual destructor
202 virtual ~FormatError() throw() {}
204 /// Set the line number
205 void line(int line) { _line = line; }
207 /// Set the error message
208 void message(const char *message) {
214 /// Set the error message
215 void message(const std::string& message) {
221 /// Set the file name
222 void file(const std::string &file) {
228 /// \brief Returns the line number
230 /// Returns the line number or zero if it was not specified.
231 int line() const { return _line; }
233 /// Returns the error message
234 const std::string& message() const {
238 /// \brief Returns the filename
240 /// Returns the filename or empty string if the filename was not
242 const std::string& file() const {
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