Skip to content
Snippets Groups Projects
exceptions.hh 6.36 KiB
Newer Older
// This file is part of the dune-xt-common project:
//   https://github.com/dune-community/dune-xt-common
René Fritze's avatar
René Fritze committed
// Copyright 2009-2018 dune-xt-common developers and contributors. All rights reserved.
// License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
//      or  GPL-2.0+ (http://opensource.org/licenses/gpl-license)
//          with "runtime exception" (http://www.dune-project.org/license.html)
René Fritze's avatar
René Fritze committed
// Authors:
Tobias Leibner's avatar
Tobias Leibner committed
//   Felix Schindler (2013 - 2019)
//   René Fritze     (2014 - 2018)
//   TiKeil          (2018)
//   Tobias Leibner  (2014, 2017 - 2018)
#ifndef DUNE_XT_COMMON_EXCEPTIONS_HH
#define DUNE_XT_COMMON_EXCEPTIONS_HH
#include <dune/common/exceptions.hh>
Felix Schindler's avatar
Felix Schindler committed
#include <dune/common/parallel/mpihelper.hh>
#include <dune/xt/common/deprecated.hh>
#  undef DUNE_THROW
/**
 *  \brief Macro to throw a colorfull exception.
 *
 *  Example:
\code
#include <dune/xt/common/exceptions.hh>
  DUNE_THROW(Exceptions::shapes_do_not_match,
             "size of a (" << a.size() << ") does not match the size of b (" << b.size() << ")!");
 *  This macro is essentially copied from dune-common with added color functionality and rank information.
 *  \param  E Exception class, derived from Dune::Exception.
 *  \param  m Message in ostream notation.
    const std::string th__red = Dune::XT::Common::terminal_supports_color() ? "\033[31m" : "";                         \
    const std::string th__brown = Dune::XT::Common::terminal_supports_color() ? "\033[33m" : "";                       \
    const std::string th__clear = Dune::XT::Common::terminal_supports_color() ? "\033[0m" : "";                        \
    E th__ex;                                                                                                          \
    std::ostringstream th__msg;                                                                                        \
    th__msg << m;                                                                                                      \
    std::ostringstream th__out;                                                                                        \
René Fritze's avatar
René Fritze committed
    th__out << th__red << #E << th__clear;                                                                             \
    if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)                                                      \
Felix Schindler's avatar
Felix Schindler committed
      th__out << " (on rank " << Dune::MPIHelper::getCollectiveCommunication().rank() << ")";                          \
    th__out << "\n";                                                                                                   \
René Fritze's avatar
René Fritze committed
    th__out << th__brown << "[" << th__clear;                                                                          \
    th__out << th__red << __func__ << th__clear;                                                                       \
    th__out << th__brown << "|" << th__clear;                                                                          \
    th__out << __FILE__ << th__brown << ":" << th__clear << th__red << __LINE__ << th__clear << th__brown << "]"       \
            << th__clear;                                                                                              \
René Fritze's avatar
René Fritze committed
      th__out << "\n" << th__brown << "=>" << th__clear << " " << th__msg.str();                                       \
    th__ex.message(th__out.str());                                                                                     \
    throw th__ex;                                                                                                      \
  } while (0)
#define DUNE_THROW_IF(condition, E, m)                                                                                 \
  do {                                                                                                                 \
    if (condition) {                                                                                                   \
      DUNE_THROW(E, m);                                                                                                \
    }                                                                                                                  \
  } while (0)
namespace XT {
namespace Common {
class CRTP_check_failed : public Dune::Exception

class shapes_do_not_match : public Dune::Exception

class index_out_of_range : public Dune::Exception
Felix Schindler's avatar
Felix Schindler committed
class you_are_using_this_wrong : public Dune::Exception
Felix Schindler's avatar
Felix Schindler committed
class wrong_input_given : public you_are_using_this_wrong
Felix Schindler's avatar
Felix Schindler committed
class requirements_not_met : public you_are_using_this_wrong
class configuration_error : public Dune::Exception
class conversion_error : public Dune::Exception
class results_are_not_as_expected : public Dune::Exception
class internal_error : public Dune::Exception
class external_error : public Dune::Exception
class you_have_to_implement_this : public Dune::NotImplemented
class debug_assertion : public Dune::Exception
class bisection_error : public wrong_input_given
class DUNE_DEPRECATED_MSG("Use Dune::XT::Functions::Exceptions:;reinterpretation_error (09.04.2018)!")
    reinterpretation_error : public Dune::Exception
class parameter_error : public Dune::Exception
class DXT_DEPRECATED_MSG("moved into dune-xt-functions Dune::XT::Functions::Exceptions (2018/03/16")
    spe10_data_file_missing : public Dune::IOError
class dependency_missing : public Dune::Exception
class this_should_not_happen : public Dune::InvalidStateException
{};

class logger_error : public Dune::Exception
{};

int handle_exception(const Dune::Exception& exp);
int handle_exception(const std::exception& exp);
#if HAVE_TBB
int handle_exception(const tbb::tbb_exception& exp);
#endif
} // namespace Common
} // namespace XT
#endif // DUNE_XT_COMMON_EXCEPTIONS_HH