Gromacs  2016.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Member Functions | Friends
gmx::ExceptionInitializer Class Reference

#include <gromacs/utility/exceptions.h>

Description

Provides information for Gromacs exception constructors.

This class exists to implement common functionality for initializing all Gromacs exceptions without having extra code in each exception class. In simple cases, it can be implicitly constructed by passing a simple string to an exception constructor. If more complex initialization is necessary, it is possible to explicitly construct an object of this type and then call other methods to add information before actually creating the exception object.

Todo:
With the exception of the reason string, information added with this class is not currently accessible through any public API, except for calling printFatalErrorMessage(), formatExceptionMessageToString() or formatExceptionMessageToFile(). This is not implemented as there is not yet need for it, and it is not clear what would be the best alternative for the access. It should be possible to refactor the internal implementation to suit the needs of such external access without requiring changes in code that throws these exceptions.

Public Member Functions

 ExceptionInitializer (const char *reason)
 Creates an initialized with the given string as the reason. More...
 
 ExceptionInitializer (const std::string &reason)
 Creates an initialized with the given string as the reason. More...
 
bool hasNestedExceptions () const
 Returns true if addCurrentExceptionAsNested() has been called. More...
 
void addCurrentExceptionAsNested ()
 Adds the currently caught exception as a nested exception. More...
 
template<class Exception >
void addNested (const Exception &ex)
 Adds the specified exception as a nested exception. More...
 

Friends

class GromacsException
 

Constructor & Destructor Documentation

gmx::ExceptionInitializer::ExceptionInitializer ( const char *  reason)
inline

Creates an initialized with the given string as the reason.

Parameters
[in]reasonDetailed reason for the exception.
Exceptions
std::bad_allocif out of memory.

This constructor is not explicit to allow constructing exceptions with a plain string argument given to the constructor without adding extra code to each exception class.

gmx::ExceptionInitializer::ExceptionInitializer ( const std::string &  reason)
inline

Creates an initialized with the given string as the reason.

Parameters
[in]reasonDetailed reason for the exception.
Exceptions
std::bad_allocif out of memory.

This constructor is not explicit to allow constructing exceptions with a plain string argument given to the constructor without adding extra code to each exception class.

Member Function Documentation

void gmx::ExceptionInitializer::addCurrentExceptionAsNested ( )
inline

Adds the currently caught exception as a nested exception.

May be called multiple times; all provided exceptions will be added in a list of nested exceptions.

Must not be called outside a catch block.

template<class Exception >
void gmx::ExceptionInitializer::addNested ( const Exception &  ex)
inline

Adds the specified exception as a nested exception.

May be called multiple times; all provided exceptions will be added in a list of nested exceptions.

This is equivalent to throwing ex and calling addCurrentExceptionAsNested() in the catch block, but potentially more efficient.

bool gmx::ExceptionInitializer::hasNestedExceptions ( ) const
inline

Returns true if addCurrentExceptionAsNested() has been called.

Provided for convenience for cases where exceptions will be added conditionally, and the caller wants to check whether any excetions were actually added.


The documentation for this class was generated from the following file: