Bayeux  3.4.1
Core Foundation library for SuperNEMO
Macros
exception.h File Reference

Utility macros for exception handling. More...

#include <stdexcept>
#include <sstream>
#include <boost/current_function.hpp>

Go to the source code of this file.

Macros

#define DT_THROW_IF(Condition, ExceptionType, Message)
 
#define DT_THROW(ExceptionType, Message)
 

Detailed Description

Utility macros for exception handling.

Exceptions are often thrown based on the value of a boolean expression. The exception, if it holds a string based message about what went wrong, should report where it was thrown from.

To help developers throwing exceptions in this fashion, datatools supplies some macros to assist in simplifying this work, and making the intent clearer in code.

Macro Definition Documentation

◆ DT_THROW

#define DT_THROW (   ExceptionType,
  Message 
)
Value:
{ \
std::stringstream sDT_THROW_ONLY; \
sDT_THROW_ONLY << "[" << BOOST_CURRENT_FUNCTION << ":" << __LINE__ << ": " << Message << "]"; \
throw ExceptionType(sDT_THROW_ONLY.str()); \
}

Throw ExceptionType with Message This macro is intended to simplify the common use case of throwing exceptions, with the exception holding a string message indicating where and why the exception was thrown. This macro takes two arguments as follows

Parameters
ExceptionTypeTypename of object to throw, the type must take a string as its constructor argument
MessageMessage to supply to ExceptionType, this must take the form of a string or output stream sequence

The Message is formatted as

[SIGNATURE:LINENUMBER: Message]

where SIGNATURE is the signature of the function from which the exception is thrown, and LINENUMBER is the line number where the throw occured.

In the simplest case it may be used as

DT_THROW(std::domain_error, "parameter i is negative");

If Message is composed from several streamable objects, it can be composed using the streaming operator:

DT_THROW(std::domain_error, "parameter i(" << i << ") is negative");

◆ DT_THROW_IF

#define DT_THROW_IF (   Condition,
  ExceptionType,
  Message 
)
Value:
{ \
if (Condition) { \
std::stringstream sDT_THROW_IF_ONLY; \
sDT_THROW_IF_ONLY << "[" << BOOST_CURRENT_FUNCTION << ":" << __LINE__ << ": " << Message << "]"; \
throw ExceptionType(sDT_THROW_IF_ONLY.str()); \
} \
}

Throw ExceptionType with Message if the expression Condition is true This macro is intended to simplify the common use case of throwing exceptions when a boolean condition is true, with the exception holding a string message indicating where and why the exception was thrown. This macro takes three arguments as follows

Parameters
ConditionBoolean expression
ExceptionTypeTypename of object to throw, the type must take a string as its constructor argument
MessageMessage to supply to ExceptionType, this must take the form of a string or output stream sequence

The Message is formatted as

[SIGNATURE:LINENUMBER: Message]

where SIGNATURE is the signature of the function from which the exception is thrown, and LINENUMBER is the line number where the throw occured.

In the simplest case it may be used as

DT_THROW_IF(i < 0, std::logic_error, "parameter i is negative");

If Message is composed from several streamable objects, it can be composed using the streaming operator:

DT_THROW_IF(i < 0, std::logic_error, "parameter i(" << i << ") is negative");