/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
/// \brief Basic exception classes and error handling.
/// \addtogroup exceptions
/// \brief Generic exception class.
/// Base class for exceptions used in LEMON.
class Exception : public std::exception {
virtual ~Exception() throw() {}
///A short description of the exception
virtual const char* what() const throw() {
return "lemon::Exception";
/// \brief Input-Output error
/// This exception is thrown when a file operation cannot be
class IoError : public Exception {
mutable std::string _what;
IoError(const IoError &error) throw() : Exception() {
explicit IoError(const char *message) throw() {
IoError::message(message);
explicit IoError(const std::string &message) throw() {
IoError::message(message);
explicit IoError(const char *message,
const std::string &file) throw() {
IoError::message(message);
explicit IoError(const std::string &message,
const std::string &file) throw() {
IoError::message(message);
virtual ~IoError() throw() {}
/// Set the error message
void message(const char *message) throw() {
/// Set the error message
void message(const std::string& message) throw() {
void file(const std::string &file) throw() {
/// Returns the error message
const std::string& message() const throw() {
/// \brief Returns the filename
/// Returns the filename or an empty string if it was not specified.
const std::string& file() const throw() {
/// \brief Returns a short error message
/// Returns a short error message which contains the message and the
virtual const char* what() const throw() {
oss << "lemon:IoError" << ": ";
oss << " ('" << _file << "')";
if (!_what.empty()) return _what.c_str();
else return "lemon:IoError";
/// This exception is thrown when an input file has wrong
/// format or a data representation is not legal.
class FormatError : public Exception {
mutable std::string _what;
FormatError(const FormatError &error) throw() : Exception() {
explicit FormatError(const char *message) throw() {
FormatError::message(message);
explicit FormatError(const std::string &message) throw() {
FormatError::message(message);
explicit FormatError(const char *message,
const std::string &file, int line = 0) throw() {
FormatError::message(message);
explicit FormatError(const std::string &message,
const std::string &file, int line = 0) throw() {
FormatError::message(message);
virtual ~FormatError() throw() {}
void line(int line) throw() { _line = line; }
/// Set the error message
void message(const char *message) throw() {
/// Set the error message
void message(const std::string& message) throw() {
void file(const std::string &file) throw() {
/// \brief Returns the line number
/// Returns the line number or zero if it was not specified.
int line() const throw() { return _line; }
/// Returns the error message
const std::string& message() const throw() {
/// \brief Returns the filename
/// Returns the filename or an empty string if it was not specified.
const std::string& file() const throw() {
/// \brief Returns a short error message
/// Returns a short error message which contains the message, the
/// file name and the line number.
virtual const char* what() const throw() {
oss << "lemon:FormatError" << ": ";
if (!_file.empty() || _line != 0) {
if (!_file.empty()) oss << "in file '" << _file << "'";
if (!_file.empty() && _line != 0) oss << " ";
if (_line != 0) oss << "at line " << _line;
if (!_what.empty()) return _what.c_str();
else return "lemon:FormatError";