add the error macros from opm-core

i.e., OPM_THROW(exception, message) and some excption classes
This commit is contained in:
Andreas Lauser 2015-10-08 11:37:59 +02:00
parent 295c848e54
commit 4a3e1a2fd9
3 changed files with 127 additions and 0 deletions

View File

@ -47,5 +47,7 @@ list (APPEND PROGRAM_SOURCE_FILES
list( APPEND PUBLIC_HEADER_FILES list( APPEND PUBLIC_HEADER_FILES
opm/common/ErrorMacros.hpp
opm/common/Exceptions.hpp
opm/common/utility/platform_dependent/disable_warnings.h opm/common/utility/platform_dependent/disable_warnings.h
opm/common/utility/platform_dependent/reenable_warnings.h ) opm/common/utility/platform_dependent/reenable_warnings.h )

View File

@ -0,0 +1,62 @@
/*
Copyright 2013 Andreas Lauser
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_ERRORMACROS_HPP
#define OPM_ERRORMACROS_HPP
#include <string>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <cassert>
// macros for reporting to stderr
#ifdef OPM_VERBOSE // Verbose mode
# include <iostream>
# define OPM_REPORT do { std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] " } while (false)
# define OPM_MESSAGE(x) do { OPM_REPORT; std::cerr << x << "\n"; } while (false)
# define OPM_MESSAGE_IF(cond, m) do {if(cond) OPM_MESSAGE(m);} while (false)
#else // non-verbose mode (default)
# define OPM_REPORT do {} while (false)
# define OPM_MESSAGE(x) do {} while (false)
# define OPM_MESSAGE_IF(cond, m) do {} while (false)
#endif
// Macro to throw an exception. NOTE: For this macro to work, the
// exception class must exhibit a constructor with the signature
// (const std::string &message). Since this condition is not fulfilled
// for the std::exception, you should use this macro with some
// exception class derived from either std::logic_error or
// std::runtime_error.
//
// Usage: OPM_THROW(ExceptionClass, "Error message " << value);
#define OPM_THROW(Exception, message) \
do { \
std::ostringstream oss__; \
oss__ << "[" << __FILE__ << ":" << __LINE__ << "] " << message; \
OPM_MESSAGE(message); \
throw Exception(oss__.str()); \
} while (false)
// throw an exception if a condition is true
#define OPM_ERROR_IF(condition, message) do {if(condition){ OPM_THROW(std::logic_error, message);}} while(false)
#endif // OPM_ERRORMACROS_HPP

63
opm/common/Exceptions.hpp Normal file
View File

@ -0,0 +1,63 @@
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* Copyright (C) 2013 by Andreas Lauser *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
/*!
* \file
* \brief Provides the OPM specific exception classes.
*/
#ifndef OPM_EXCEPTIONS_HPP
#define OPM_EXCEPTIONS_HPP
#include <stdexcept>
// the OPM-specific exception classes
namespace Opm {
class NotImplemented : public std::logic_error
{
public:
explicit NotImplemented(const std::string &message)
: std::logic_error(message)
{}
};
class NumericalProblem : public std::runtime_error
{
public:
explicit NumericalProblem(const std::string &message)
: std::runtime_error(message)
{}
};
class MaterialLawProblem : public NumericalProblem
{
public:
explicit MaterialLawProblem(const std::string &message)
: NumericalProblem(message)
{}
};
class LinearSolverProblem : public NumericalProblem
{
public:
explicit LinearSolverProblem(const std::string &message)
: NumericalProblem(message)
{}
};
}
#endif // OPM_EXCEPTIONS_HPP