/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2008
* 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() {}
///\e 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) {
explicit IoError(const char *message) {
IoError::message(message);
explicit IoError(const std::string &message) {
IoError::message(message);
IoError(const std::string &file, const char *message) {
IoError::message(message);
IoError(const std::string &file, const std::string &message) {
IoError::message(message);
virtual ~IoError() throw() {}
/// Set the error message
void message(const char *message) {
/// Set the error message
void message(const std::string& message) {
void file(const std::string &file) {
/// Returns the error message
const std::string& message() const {
/// \brief Returns the filename
/// Returns the filename or empty string if the filename was not
const std::string& file() const {
/// \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:IoError" << ": ";
if (!file().empty()) oss << "with file '" << file() << "'";
if (!_what.empty()) return _what.c_str();
else return "lemon:IoError";
/// This class is used to indicate if an input file has wrong
/// formatting, or a data representation is not legal.
class FormatError : public Exception {
mutable std::string _what;
FormatError(const FormatError &error) {
explicit FormatError(const char *message) {
FormatError::message(message);
explicit FormatError(const std::string &message) {
FormatError::message(message);
FormatError(const std::string &file, int line, const char *message) {
FormatError::message(message);
FormatError(const std::string &file, int line, const std::string &message) {
FormatError::message(message);
virtual ~FormatError() throw() {}
void line(int line) { _line = line; }
/// Set the error message
void message(const char *message) {
/// Set the error message
void message(const std::string& message) {
void file(const std::string &file) {
/// \brief Returns the line number
/// Returns the line number or zero if it was not specified.
int line() const { return _line; }
/// Returns the error message
const std::string& message() const {
/// \brief Returns the filename
/// Returns the filename or empty string if the filename was not
const std::string& file() const {
/// \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";