2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-30 11:07:39 +02:00
|
|
|
.. highlight:: c
|
|
|
|
|
|
|
|
.. _util_exceptions:
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
Exceptions
|
2013-08-31 00:04:09 +02:00
|
|
|
=============
|
2013-08-30 11:07:39 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
TRIQS defines special exceptions, with the following characteristics :
|
|
|
|
|
2013-08-31 00:04:09 +02:00
|
|
|
* they derives from std::exceptions
|
2013-07-17 19:24:07 +02:00
|
|
|
* their .what() contains :
|
|
|
|
|
|
|
|
* 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
|
|
|
|
* a complete stack strace of the C++ code at the exception point, with demangling of the function name (on gcc and clang).
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
For uniformity, it is highly recommended to use these macros when developing for TRIQS.
|
|
|
|
|
2013-08-31 00:04:09 +02:00
|
|
|
Example :
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-31 00:04:09 +02:00
|
|
|
.. compileblock::
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-31 00:04:09 +02:00
|
|
|
// automatically included in e.g. arrays, gfs, any triqs library...
|
|
|
|
#include <triqs/utility/exceptions.hpp>
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
if (2!=3) TRIQS_RUNTIME_ERROR <<" The condition is false because "<< 2 << "!=" << 3;
|
|
|
|
}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-31 00:04:09 +02:00
|
|
|
The exception can of course be caught :
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-31 00:04:09 +02:00
|
|
|
.. compileblock::
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-08-31 00:04:09 +02:00
|
|
|
// 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) {
|
2013-07-17 19:24:07 +02:00
|
|
|
std::cout << "caught error "<< e.what()<<std::endl;
|
2013-08-31 00:04:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
f();
|
|
|
|
}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|