cepgen is hosted by Hepforge, IPPP Durham
CepGen N/A
Central exclusive processes event generator
Exception.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2025 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Core_Exception_h
20#define CepGen_Core_Exception_h
21
23
24namespace cepgen {
26 class Exception : public LoggedMessage, public std::runtime_error {
27 public:
29 enum struct Type {
30 undefined = -1,
31 error,
32 fatal
33 };
34 explicit Exception(const std::string& module_name,
35 const std::string& from = "",
37 const std::string& file = "",
38 short lineno = 0) noexcept(true);
39 Exception(const Exception&) noexcept(true);
40 ~Exception() noexcept override;
41
42 friend std::ostream& operator<<(std::ostream&, const Type&);
43
44 template <typename T>
45 friend const Exception& operator<<(const Exception& exception, const T& message) noexcept {
46 static_cast<const LoggedMessage&>(exception) << message;
47 return exception;
48 }
49 void dump(std::ostream* = nullptr) const noexcept(true) override;
50 const char* what() const noexcept(true) override;
51
52 private:
53 const Type type_{Type::undefined};
54 mutable char what_[50]{""};
55 };
56 static_assert(std::is_nothrow_copy_constructible_v<Exception>, "Exception must be nothrow copy-constructible");
57} // namespace cepgen
58
59#define CG_ERROR(mod) cepgen::Exception(mod, __FUNC__, cepgen::Exception::Type::error, __FILE__, __LINE__)
60#define CG_FATAL(mod) cepgen::Exception(mod, __FUNC__, cepgen::Exception::Type::fatal, __FILE__, __LINE__)
61#define CG_ASSERT(assertion) \
62 if (!(assertion)) \
63 throw CG_FATAL("Assertion") << "Assertion '" << #assertion << "' failed.";
64
65#endif
Standard exception message.
Definition Exception.h:26
const char * what() const noexcept(true) override
Type
Enumeration of exception severities.
Definition Exception.h:29
@ undefined
Irregular message.
@ error
General non-stopping error.
@ fatal
Critical and stopping error.
Exception(const std::string &module_name, const std::string &from="", Type type=Type::undefined, const std::string &file="", short lineno=0) noexcept(true)
void dump(std::ostream *=nullptr) const noexcept(true) override
Human-readable dump of the exception.
A simple exception handler.
Definition Message.h:43
const std::string & from() const
Origin of the exception.
Definition Message.h:152
const MessageType & type() const
Message type.
Definition Message.h:155
std::string message() const
Human-readable message.
Definition Message.h:151
const std::string & file() const
File where the exception occurred.
Definition Message.h:153
Common namespace for this Monte Carlo generator.
Definition Handler.h:26