TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2022 Simons Foundation
4// Copyright (c) 2015 Igor Krivenko
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You may obtain a copy of the License at
17// https://www.gnu.org/licenses/gpl-3.0.txt
18//
19// Authors: Igor Krivenko, Olivier Parcollet, Nils Wentzell
20
25
26#pragma once
27
28#include "./macros.hpp"
29
30#include <mpi/mpi.hpp>
31
32#include <cstdlib>
33#include <exception>
34#include <sstream>
35#include <string>
36
41
43#define TRIQS_ERROR(CLASS, NAME) throw CLASS() << "Triqs " << NAME << "\n at " << __FILE__ << " : " << __LINE__ << "\n\n"
44
46#define TRIQS_RUNTIME_ERROR TRIQS_ERROR(triqs::runtime_error, "runtime error")
47
49#define TRIQS_KEYBOARD_INTERRUPT TRIQS_ERROR(triqs::keyboard_interrupt, "Ctrl-C")
50
52#define TRIQS_ASSERT(X) \
53 if (!(X)) TRIQS_RUNTIME_ERROR << AS_STRING(X);
54
56#define TRIQS_ASSERT2(X, ...) \
57 if (!(X)) TRIQS_RUNTIME_ERROR << AS_STRING(X) << "\n " << __VA_ARGS__;
58
60
61namespace triqs {
62
67
75 class exception : public std::exception {
76 std::stringstream acc;
77 std::string _trace;
78 mutable std::string _what;
79
80 public:
82 exception() noexcept : std::exception() {}
83
90 exception(exception const &e) noexcept : acc(e.acc.str()), _trace(e._trace), _what(e._what) {}
91
93 virtual ~exception() noexcept {}
94
104 template <typename T> exception &operator<<(T const &x) {
105 acc << x;
106 return *this;
107 }
108
115 exception &operator<<(const char *mess) {
116 (*this) << std::string(mess);
117 return *this;
118 }
119
129 virtual const char *what() const noexcept {
130 std::stringstream out;
131 out << acc.str() << "\nException was thrown on node ";
132 if (mpi::is_initialized()) out << mpi::communicator().rank() << "\n";
133 if (getenv("TRIQS_SHOW_EXCEPTION_TRACE")) out << ".. C++ trace is : " << trace() << "\n";
134 _what = out.str();
135 return _what.c_str();
136 }
137
142 virtual const char *trace() const noexcept { return _trace.c_str(); }
143 };
144
151 class runtime_error : public exception {
152 public:
154 runtime_error() noexcept : exception() {}
155
157 virtual ~runtime_error() noexcept {}
158
166 template <typename T> runtime_error &operator<<(T const &x) {
168 return *this;
169 }
170 };
171
179 public:
182
184 virtual ~keyboard_interrupt() noexcept {}
185
193 template <typename T> keyboard_interrupt &operator<<(T const &x) {
195 return *this;
196 }
197 };
198
200
201} // namespace triqs
exception & operator<<(T const &x)
Append a value to the accumulated diagnostic message.
exception(exception const &e) noexcept
Copy constructor copies the accumulated diagnostic message, the captured stack trace and the cached w...
virtual const char * trace() const noexcept
Get the captured stack trace.
exception() noexcept
Default constructor ceates an exception with an empty diagnostic message and an empty stack trace.
virtual ~exception() noexcept
Virtual default destructor.
exception & operator<<(const char *mess)
Append a C string to the accumulated diagnostic message.
virtual const char * what() const noexcept
Build and return the full diagnostic message.
keyboard_interrupt & operator<<(T const &x)
Append a value to the accumulated diagnostic message.
virtual ~keyboard_interrupt() noexcept
Virtual default destructor.
keyboard_interrupt() noexcept
Default constructor creates a keyboard interrupt with an empty diagnostic message.
virtual ~runtime_error() noexcept
Virtual default destructor.
runtime_error() noexcept
Default constructor creates a runtime error with an empty diagnostic message.
runtime_error & operator<<(T const &x)
Append a value to the accumulated diagnostic message.
Common macros used in TRIQS.