Bitcoin Core  21.99.0
P2P Digital Currency
Classes | Macros | Functions
check.h File Reference
#include <tinyformat.h>
#include <stdexcept>
Include dependency graph for check.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  NonFatalCheckError
 

Macros

#define CHECK_NONFATAL(condition)
 Throw a NonFatalCheckError when the condition evaluates to false. More...
 
#define Assert(val)   ([&]() -> decltype(get_pure_r_value(val)) { auto&& check = (val); assert(#val && check); return std::forward<decltype(get_pure_r_value(val))>(check); }())
 Identity function. More...
 
#define Assume(val)   ((void)(val))
 Assume is the identity function. More...
 

Functions

template<typename T >
get_pure_r_value (T &&val)
 Helper for Assert() More...
 

Macro Definition Documentation

◆ Assert

#define Assert (   val)    ([&]() -> decltype(get_pure_r_value(val)) { auto&& check = (val); assert(#val && check); return std::forward<decltype(get_pure_r_value(val))>(check); }())

Identity function.

Abort if the value compares equal to zero

Definition at line 57 of file check.h.

◆ Assume

#define Assume (   val)    ((void)(val))

Assume is the identity function.

  • Should be used to run non-fatal checks. In debug builds it behaves like Assert()/assert() to notify developers and testers about non-fatal errors. In production it doesn't warn or log anything.
  • For fatal errors, use Assert().
  • For non-fatal errors in interactive sessions (e.g. RPC or command line interfaces), CHECK_NONFATAL() might be more appropriate.

Definition at line 72 of file check.h.

◆ CHECK_NONFATAL

#define CHECK_NONFATAL (   condition)
Value:
do { \
if (!(condition)) { \
strprintf("%s:%d (%s)\n" \
"Internal bug detected: '%s'\n" \
"You may report this issue here: %s\n", \
__FILE__, __LINE__, __func__, \
(#condition), \
} \
} while (false)
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
#define PACKAGE_BUGREPORT

Throw a NonFatalCheckError when the condition evaluates to false.

This should only be used

  • where the condition is assumed to be true, not for error handling or validating user input
  • where a failure to fulfill the condition is recoverable and does not abort the program

For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC caller, which can then report the issue to the developers.

Definition at line 32 of file check.h.

Function Documentation

◆ get_pure_r_value()

template<typename T >
T get_pure_r_value ( T &&  val)

Helper for Assert()

Definition at line 51 of file check.h.