1. Exceptions
TRIQS defines special exceptions, with the following characteristics :
- they derives from std::exceptions
- their method .what() returns :
- the date and time of the exception (useful in long QMC computations…).
- the file and line where the exception occurred
- an additionnal error message (see example below). The error behaves like a std::stringstream, one can accumulate any message
- their method .what() returns : * a complete stack strace of the C++ code at the exception point, with demangling of the function name (on gcc and clang).
In addition, we provide a TRIQS_RUNTIME_ERROR macro to throw exception easily, Cf example below.
Note
For uniformity, it is highly recommended to use these macros when developing for TRIQS.
Example:
// automatically included in e.g. arrays, gfs, any triqs library...
#include <triqs/utility/exceptions.hpp>
int main() {
try {
if (2 != 3) TRIQS_RUNTIME_ERROR << " The condition is false because " << 2 << "!=" << 3;
} catch (...) {}
}
The exception can of course be caught :
// automatically included in e.g. arrays, gfs, any triqs library...
#include <triqs/utility/exceptions.hpp>
#include <iostream>
void f() {
try {
if (2 != 3) TRIQS_RUNTIME_ERROR << " The condition is false because " << 2 << "!=" << 3;
} catch (triqs::runtime_error const &e) {
std::cout << "caught error \n" << e.what() << std::endl;
std::cout << "C++ trace = \n" << e.trace() << std::endl;
}
}
int main() { f(); }
caught error
Triqs runtime error
at /src/triqs/doc/documentation/manual/triqs/utilities/exceptions_1.cpp : 7
The condition is false because 2!=3
Exception was thrown on node
C++ trace =