Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
Exception.hpp File Reference
#include <string>
#include <sstream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <exception>
#include <boost/preprocessor/stringize.hpp>
+ Include dependency graph for Exception.hpp:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Exception
 

Macros

#define EXCEPTION(message)
 
#define EXCEPT_IF_NOT(test)    if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
 
#define EXCEPT_IF(test)    if (test) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
 
#define TERMINATE(message)
 
#define NEVER_REACHED   TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)
 
#define UNUSED_OPT(var)
 
#define ABORT_IF_THROWS(block)
 
#define EXPECT0(cmd, arg)
 
#define ABORT_IF_NON0_WITH_MSG(retcode, msg)
 
#define ABORT_IF_NON0(cmd, arg)
 
#define EXPECT_NON0(cmd, arg)
 
#define IGNORE_RET(cmd, arg)
 

Variables

const unsigned UNSIGNED_UNSET = UINT_MAX
 
const int INT_UNSET = INT_MAX
 
const double DOUBLE_UNSET = DBL_MAX
 

Detailed Description

Contains the Exception class, along with some macros that are widely used throughout the code.

Definition in file Exception.hpp.

Macro Definition Documentation

◆ ABORT_IF_NON0

#define ABORT_IF_NON0 (   cmd,
  arg 
)
Value:
{ \
std::string _arg(arg); \
int ret = cmd(_arg.c_str()); \
ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
}
Note
This macro is deprecated, and no longer used by core code.

Handy for calling functions like system which return non-zero on error. Terminate if an error occurs.

This macro should be used instead of EXPECT0 within blocks that are only executed by one process, but need to kill all processes if an error occurs.

Parameters
cmdcommand to call
argits argument (will be converted to std::string)

Definition at line 308 of file Exception.hpp.

◆ ABORT_IF_NON0_WITH_MSG

#define ABORT_IF_NON0_WITH_MSG (   retcode,
  msg 
)
Value:
if (retcode != 0) { \
TERMINATE(msg); \
}
Note
This macro is deprecated, and no longer used by core code.

Handy for calling functions like system which return non-zero on error. Terminate if the return code is non-zero, printing a suitable message.

Parameters
retcodecommand return code
msgerror message to display

Definition at line 291 of file Exception.hpp.

◆ ABORT_IF_THROWS

#define ABORT_IF_THROWS (   block)
Value:
try { \
block; \
} catch (const Exception& e) { \
TERMINATE(e.GetMessage()); \
} catch (const std::exception &e) { \
TERMINATE(e.what()); \
} catch (...) { \
TERMINATE("Unexpected exception thrown."); \
}

Convenience function to convert an exception thrown by a single process into termination of the entire program.

Parameters
blockthe block of code to execute

Definition at line 246 of file Exception.hpp.

◆ EXCEPT_IF

#define EXCEPT_IF (   test)     if (test) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))

Convenience macro for the opposite truth condition as EXCEPT_IF_NOT.

Parameters
testthe test that must always be false.

Definition at line 179 of file Exception.hpp.

◆ EXCEPT_IF_NOT

#define EXCEPT_IF_NOT (   test)     if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))

Convenience macro for changing an assert into an exception - has the same calling semantics, but throws.

Parameters
testthe test that must always be true.

Definition at line 171 of file Exception.hpp.

◆ EXCEPTION

#define EXCEPTION (   message)
Value:
do { \
std::stringstream msg_stream; \
msg_stream << message; \
throw Exception(msg_stream.str(), __FILE__, __LINE__); \
} while (false)

Convenience macro for throwing an exception, in order to add file and line info.

Parameters
messagethe error message to use, as a streamed expression

Definition at line 156 of file Exception.hpp.

◆ EXPECT0

#define EXPECT0 (   cmd,
  arg 
)
Value:
{ \
std::string _arg(arg); \
int retcode = cmd(_arg.c_str()); \
if (retcode != 0) { \
EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
} }
Note
This macro is deprecated.

Handy for calling functions like system which return non-zero on error. Throws if an error occurs.

Note
DO NOT use this macro within an if (PetscTools::AmMaster) block, as then you'll get deadlock if an exception is thrown when running in parallel! (Unless the block is wrapped in a try-catch and exception replication handler.) Instead, use ABORT_IF_NON0.
Parameters
cmdcommand to call
argits argument (will be converted to std::string)

Definition at line 275 of file Exception.hpp.

◆ EXPECT_NON0

#define EXPECT_NON0 (   cmd,
  arg 
)
Value:
{ \
std::string _arg = (arg); \
int ret = cmd(_arg.c_str()); \
if (ret == 0) { \
EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
} }
Note
This macro is deprecated, and no longer used by core code.

Handy for calling functions like system which return non-zero on error. This time we expect failure; throws if the command succeeds.

Parameters
cmdcommand to call
argits argument (will be converted to std::string)

Definition at line 322 of file Exception.hpp.

◆ IGNORE_RET

#define IGNORE_RET (   cmd,
  arg 
)
Value:
{ \
std::string _arg = (arg); \
int ret = cmd(_arg.c_str()); \
ret = ret; \
}
Note
This macro is deprecated.

Handy for calling functions like system which return non-zero on error. This version ignores the return code, in case you don't care about errors for some reason...

Parameters
cmdcommand to call
argits argument (will be converted to std::string)

Definition at line 337 of file Exception.hpp.

◆ NEVER_REACHED

#define NEVER_REACHED   TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)

Use for control paths that will never be executed, just to make sure they aren't.

The exit statement at the end of NEVER_REACHED is not really needed but prevents g++ from complaining about uninitialised variables when you have code that looks like:

RelativeTo::Value relative_to;
switch (rPath.relative_to())
{
case cp::relative_to_type::cwd:
relative_to = RelativeTo::CWD;
break;
case cp::relative_to_type::chaste_test_output:
break;
case cp::relative_to_type::chaste_source_root:
break;
case cp::relative_to_type::absolute:
relative_to = RelativeTo::Absolute;
break;
default:
break;
}
#define NEVER_REACHED

relative_to is considered potentially uninitialised in the default branch unless the compiler finds a exit, assert or throw statement.

Definition at line 227 of file Exception.hpp.

◆ TERMINATE

#define TERMINATE (   message)
Value:
do { \
std::stringstream msg_stream; \
msg_stream << message; \
Exception::Terminate(msg_stream.str(), __FILE__, __LINE__); \
} while (false)

Terminate execution safely, even when running in parallel. Use for level 4 errors: execution cannot continue from this point and hence should be terminated (even when running with NDEBUG).

Parameters
messageexplanatory message

Definition at line 189 of file Exception.hpp.

◆ UNUSED_OPT

#define UNUSED_OPT (   var)

This is to cope with NDEBUG causing variables to not be used, when they are only used in assert()s.

Parameters
varthe "unused" variable

Definition at line 237 of file Exception.hpp.

Variable Documentation

◆ DOUBLE_UNSET

const double DOUBLE_UNSET = DBL_MAX

Use when initialising a double variable that doesn't have a sensible default value.

Definition at line 57 of file Exception.hpp.

Referenced by ImmersedBoundaryPalisadeMeshGenerator::ImmersedBoundaryPalisadeMeshGenerator(), OffLatticeRandomFieldGenerator< SPACE_DIM >::OffLatticeRandomFieldGenerator(), QuadraturePointsGroup< DIM >::QuadraturePointsGroup(), CellProperties::CalculateActionPotentialDurations(), AbstractCvodeCell::Compute(), ImmersedBoundaryMesh< ELEMENT_DIM, SPACE_DIM >::DivideElement(), ImmersedBoundaryMesh< ELEMENT_DIM, SPACE_DIM >::GetAverageNodeSpacingOfElement(), ImmersedBoundaryMesh< ELEMENT_DIM, SPACE_DIM >::GetAverageNodeSpacingOfLamina(), AbstractCellWriter< ELEMENT_DIM, SPACE_DIM >::GetCellDataForVtkOutput(), CellCycleModelOdeHandler::GetDt(), AbstractCvodeCellWithDataClamp::GetExperimentalVoltageAtTimeT(), AbstractOdeBasedCellCycleModel::GetOdeStopTime(), AbstractOdeBasedPhaseBasedCellCycleModel::GetOdeStopTime(), SimulationTime::GetTime(), AbstractCellWriter< ELEMENT_DIM, SPACE_DIM >::GetVectorCellDataForVtkOutput(), CellAppliedForceWriter< ELEMENT_DIM, SPACE_DIM >::GetVectorCellDataForVtkOutput(), SimulationTime::IsStartTimeSetUp(), SimulationTime::ResetEndTimeAndNumberOfTimeSteps(), SimulationTime::SetEndTimeAndNumberOfTimeSteps(), Kerchoffs2003ContractionModel::SetInputParameters(), Nash2004ContractionModel::SetInputParameters(), NhsContractionModel::SetInputParameters(), SimulationTime::SetStartTime(), AbstractCvodeCell::SolveAndUpdateState(), LinearParabolicPdeSystemWithCoupledOdeSystemSolver< ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM >::SolveAndWriteResultsToFile(), ContactInhibitionCellCycleModel::UpdateCellCyclePhase(), NormallyDistributedTargetAreaModifier< DIM >::UpdateTargetAreaOfCell(), SimpleTargetAreaModifier< DIM >::UpdateTargetAreaOfCell(), TargetAreaLinearGrowthModifier< DIM >::UpdateTargetAreaOfCell(), AbstractHdf5Converter< ELEMENT_DIM, SPACE_DIM >::WriteInfoFile(), and ImmersedBoundaryCellPopulation< DIM >::WriteVtkResultsToFile().

◆ INT_UNSET

const int INT_UNSET = INT_MAX

Use when initialising an int variable that doesn't have a sensible default value.

Definition at line 55 of file Exception.hpp.

◆ UNSIGNED_UNSET

const unsigned UNSIGNED_UNSET = UINT_MAX

Use when initialising an unsigned variable that doesn't have a sensible default value.

Definition at line 53 of file Exception.hpp.

Referenced by CellId::CellId(), CellProperties::CalculateActionPotentialDurations(), ExclusionCaBasedDivisionRule< SPACE_DIM >::CalculateDaughterNodeIndex(), ShovingCaBasedDivisionRule< SPACE_DIM >::CalculateDaughterNodeIndex(), CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::DetermineWatchedNodes(), NodePartitioner< ELEMENT_DIM, SPACE_DIM >::GeometricPartitioning(), Cell::GetAncestor(), CellAncestorWriter< ELEMENT_DIM, SPACE_DIM >::GetCellDataForVtkOutput(), CellId::GetCellId(), VertexMesh< ELEMENT_DIM, SPACE_DIM >::GetDelaunayNodeIndexCorrespondingToVoronoiElementIndex(), CellId::GetMaxCellId(), VertexMesh< ELEMENT_DIM, SPACE_DIM >::GetVoronoiElementIndexCorrespondingToDelaunayNodeIndex(), SimpleWntCellCycleModel::GetWntType(), CellCycleModelOdeSolver< CELL_CYCLE_MODEL, BackwardEulerIvpOdeSolver >::Initialise(), CellCycleModelOdeSolver< CELL_CYCLE_MODEL, BackwardEulerIvpOdeSolver >::IsSetUp(), AbstractHdf5Converter< ELEMENT_DIM, SPACE_DIM >::MoveOntoNextDataset(), CellCycleModelOdeSolver< CELL_CYCLE_MODEL, BackwardEulerIvpOdeSolver >::Reset(), AbstractConductivityModifier< ELEMENT_DIM, SPACE_DIM >::rGetModifiedConductivityTensor(), AbstractCellCycleModel::SetDimension(), CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::Solve(), MutableMesh< ELEMENT_DIM, SPACE_DIM >::SplitEdge(), and CellAncestorWriter< ELEMENT_DIM, SPACE_DIM >::VisitCell().