DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
Exceptions.h File Reference
#include <string>
#include <stdexcept>
#include <assert.h>
#include "Exceptions.t.cc"

Go to the source code of this file.

Namespaces

namespace  dftfe
 
namespace  dftfe::utils
 

Macros

#define DFTFE_Assert(expr)
 provides an interface for exception handling. It two overrides on the assert(expr) function in C/C++ and two wrappers on std::exception.
 
#define DFTFE_AssertWithMsg(expr, msg)
 
#define MPICHECK(cmd)
 

Typedefs

typedef std::logic_error dftfe::utils::LogicError
 
typedef std::invalid_argument dftfe::utils::InvalidArgument
 
typedef std::domain_error dftfe::utils::DomainError
 
typedef std::length_error dftfe::utils::LengthError
 
typedef std::out_of_range dftfe::utils::OutOfRangeError
 
typedef std::runtime_error dftfe::utils::RuntimeError
 
typedef std::overflow_error dftfe::utils::OverflowError
 
typedef std::underflow_error dftfe::utils::UnderflowError
 

Functions

void dftfe::utils::throwException (bool condition, std::string msg="")
 
template<class T>
void dftfe::utils::throwException (bool condition, std::string msg="")
 

Macro Definition Documentation

◆ DFTFE_Assert

#define DFTFE_Assert ( expr)
Value:
assert(expr)

provides an interface for exception handling. It two overrides on the assert(expr) function in C/C++ and two wrappers on std::exception.

The overrides on assert(expr) are useful for debug mode testing. That is, you want the assert to be executed only in debug mode and not in release mode (i.e., if NDEBUG is defined). The two assert overrides are

  1. DFTFE_Assert(expr): same as std::assert(expr). Throws an assert if expr is false
  2. DFTFE_AssertWithMsg(expr,msg): same as above but takes an additional message in the form of string to display if expr is false.

It also provides two preprocessor flags, DFTFE_DISABLE_ASSERT and DFTFE_ENABLE_ASSERT, that you can set to override the NDEBUG flag in a particular source file. This is provided to allow selective enabling or disabling of Assert and AssertWithMsg without any relation to whether NDEBUG is defined or not (NDEBUG is typically defined globally for all files through compiler options). For example, if in a file you have define DFTFE_DISABLE_ASSERT include "Exceptions.h" then it would disable all calls to Assert or AssertWithMsg in that file, regardless of whether NDEBUG is defined. Also, it has no bearing on std::assert (i.e., any calls to std::assert in that file will still be governed by NDEBUG). Similarly, if in a file you have define DFTFE_ENABLE_ASSERT include "Exceptions.h" then it would enable all calls to Assert or AssertWithMsg in that file, regardless of whether NDEBUG is defined. Also, it has no bearning on std::assert (i.e., any calls to std::assert in that file will still be governed by NDEBUG)

It also provides two wrappers on std::exception and its derived classes (e.g., std::runtime_error, std::domain_error, etc.) The two wrappers are:

  1. dftfe::utils::throwException(expr,msg): a generic exception handler which throws an optional message (msg) if expr evaluates to false. It combines std::exception with an additional messgae. (Note: the std::exception has no easy way of taking in a message).
  2. dftfe::utils::throwException<T>(expr, msg): similar to the above, but takes a specific derived class of std::exception handler as a template parameter. The derived std::exception must have a constructor that takes in a string. For the ease of the user, we have typedef-ed some commonly used derived classes of std::exception. A user can use the typedefs as the template parameter instead. Available typedefs LogicError - std::logic_error InvalidArgument - std::invalid_argument DomainError - std::domain_error LengthError - std::length_error OutOfRangeError - std::out_of_range FutureError - std::future_error RuntimeError - std::runtime_error OverflowError - std::overflow_error UnderflowError - std::underflow_error

◆ DFTFE_AssertWithMsg

#define DFTFE_AssertWithMsg ( expr,
msg )
Value:
assert((expr) && (msg))

◆ MPICHECK

#define MPICHECK ( cmd)
Value:
do \
{ \
int e = cmd; \
if (e != MPI_SUCCESS) \
{ \
printf("Failed: MPI error %s:%d '%d'\n", __FILE__, __LINE__, e); \
exit(EXIT_FAILURE); \
} \
} \
while (0)