error Class Reference

Class to handle errors and exceptions in a simple, consistent stream-based manner. More...

Inheritance diagram for error:
[legend]
Collaboration diagram for error:
[legend]

Public Member Functions

 error (const string &title)
 Construct from title string. More...
 
 error (const dictionary &errDict)
 Construct from dictionary. More...
 
 error (const error &err)
 Copy construct. More...
 
virtual ~error () noexcept
 Destructor. More...
 
string message () const
 The accumulated error message. More...
 
void clear () const
 Clear any messages. More...
 
const stringfunctionName () const noexcept
 The currently defined function name for output messages. More...
 
const stringsourceFileName () const noexcept
 The currently defined source-file name for output messages. More...
 
label sourceFileLineNumber () const noexcept
 The currently defined source-file line number for output messages. More...
 
bool throwing () const noexcept
 Return the current exception throwing state (on or off) More...
 
bool throwing (const bool on) noexcept
 Specify exception throwing state (on or off) More...
 
OSstreamstream ()
 Return OSstream for output operations. More...
 
 operator OSstream & ()
 Implicit cast to OSstream for << operations. More...
 
OSstreamoperator() ()
 Explicit convert to OSstream for << operations. More...
 
OSstreamoperator() (const string &functionName)
 Define basic print message. More...
 
OSstreamoperator() (const char *functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
 Define basic print message. More...
 
OSstreamoperator() (const string &functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
 Define basic print message. More...
 
 operator dictionary () const
 Extract a dictionary representation of the error information. More...
 
void exit (const int errNo=1)
 Exit : can be called for any error to exit program. More...
 
void abort ()
 Abort : used to stop code for fatal errors. More...
 
virtual void write (Ostream &os, const bool withTitle=true) const
 Print error message. More...
 
bool throwExceptions (const bool on=true) noexcept
 Specify exception throwing state (default is on) More...
 
bool dontThrowExceptions () noexcept
 Deactivate exception throwing. More...
 
- Public Member Functions inherited from messageStream
 messageStream (const string &title, const errorSeverity severity, const int maxErrors=0)
 Construct from components. More...
 
 messageStream (const dictionary &dict)
 Construct as Fatal from dictionary, extracting the 'title'. More...
 
const stringtitle () const noexcept
 The title of this error type. More...
 
int maxErrors () const noexcept
 The maximum number of errors before program termination. More...
 
int maxErrors (int nErrors) noexcept
 Set the maximum number of errors before program termination. More...
 
OSstreamstream (OSstream *alternative=nullptr)
 
OSstreammasterStream (const label communicator)
 
std::ostream & stdStream ()
 Return std::ostream for output operations. More...
 
 operator OSstream & ()
 Implicit cast to OSstream for << operations. More...
 
OSstreamoperator() ()
 Explicitly convert to OSstream for << operations. More...
 
OSstreamoperator() (const string &functionName)
 Report 'From function-name'. More...
 
OSstreamoperator() (const char *functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
 
OSstreamoperator() (const string &functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
 Report 'From function-name, source file, line number'. More...
 
OSstreamoperator() (const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const string &ioFileName, const label ioStartLineNumber=-1, const label ioEndLineNumber=-1)
 
OSstreamoperator() (const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const IOstream &)
 
OSstreamoperator() (const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const dictionary &)
 

Static Public Member Functions

static bool master (const label communicator=-1)
 
static bool warnAboutAge (const int version) noexcept
 Test if an age warning should be emitted. More...
 
static bool warnAboutAge (const char *what, const int version)
 Emit warning on stderr about something being old. More...
 
static void safePrintStack (std::ostream &os)
 
static void printStack (Ostream &os)
 Helper function to print a stack. More...
 
static bool useAbort ()
 True if FOAM_ABORT is on. More...
 

Protected Member Functions

void simpleExit (const int errNo, const bool isAbort)
 Exit or abort, without throwing or job control handling. More...
 

Protected Attributes

string functionName_
 
string sourceFileName_
 
label sourceFileLineNumber_
 
bool throwing_
 
std::unique_ptr< OStringStreammessageStreamPtr_
 
- Protected Attributes inherited from messageStream
string title_
 
errorSeverity severity_
 
int maxErrors_
 
int errorCount_
 

Additional Inherited Members

- Public Types inherited from messageStream
enum  errorSeverity {
  INFO = 1 , INFO_STDERR , WARNING , SERIOUS ,
  FATAL
}
 Message type, error severity flags. More...
 
- Static Public Attributes inherited from messageStream
static int level
 The output level (verbosity) of messages. More...
 
static int redirect
 The output redirection of messages. More...
 

Detailed Description

Class to handle errors and exceptions in a simple, consistent stream-based manner.

The error class is globally instantiated with a title string. Errors, messages and other data are piped to the messageStream class in the standard manner. Manipulators are supplied for exit and abort that may terminate the program or throw an exception depending on whether the exception handling has been switched on (off by default).

Usage
error << "message1" << "message2" << FoamDataType << exit(errNo);
error << "message1" << "message2" << FoamDataType << abort();
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:77
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:331
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:337
Source files
See also
Foam::errorManip Foam::errorManipArg

Definition at line 73 of file error.H.

Constructor & Destructor Documentation

◆ error() [1/3]

error ( const string title)
explicit

Construct from title string.

Definition at line 107 of file error.C.

◆ error() [2/3]

error ( const dictionary errDict)
explicit

Construct from dictionary.

Definition at line 119 of file error.C.

◆ error() [3/3]

error ( const error err)

Copy construct.

Definition at line 131 of file error.C.

◆ ~error()

~error ( )
virtualnoexcept

Destructor.

Definition at line 145 of file error.C.

Member Function Documentation

◆ simpleExit()

void simpleExit ( const int  errNo,
const bool  isAbort 
)
protected

Exit or abort, without throwing or job control handling.

Definition at line 253 of file error.C.

References UPstream::abort(), Foam::endl(), JobInfo::exit(), Foam::nl, UPstream::parRun(), Foam::Perr, error::printStack(), and error::useAbort().

Here is the call graph for this function:

◆ master()

bool master ( const label  communicator = -1)
static

Like Pstream::master but with a Pstream::parRun guard in case Pstream has not yet been initialised.

Parameters
communicatoris the numbered MPI communicator. By default it uses UPstream::worldComm

Definition at line 41 of file error.C.

References splitCell::master(), and UPstream::parRun().

Here is the call graph for this function:

◆ warnAboutAge() [1/2]

bool warnAboutAge ( const int  version)
staticnoexcept

Test if an age warning should be emitted.

Parameters
versionis the old version (YYMM) for determining the age in months compared to the current OpenFOAM version as conveyed by the foamVersion::api value.

Definition at line 55 of file error.C.

References Foam::foamVersion::api.

Referenced by dictionary::csearchCompat(), Foam::expandLeadingTilde(), timeControl::read(), searchableSurfaceCollection::searchableSurfaceCollection(), and Foam::subDictCompat().

Here is the caller graph for this function:

◆ warnAboutAge() [2/2]

bool warnAboutAge ( const char *  what,
const int  version 
)
static

Emit warning on stderr about something being old.

Parameters
whatdescription for the warning
versionis the old version (YYMM) for determining the age in months compared to the current OpenFOAM version as conveyed by the foamVersion::api value.

Definition at line 62 of file error.C.

References Foam::foamVersion::api.

◆ message()

Foam::string message ( ) const

The accumulated error message.

Definition at line 319 of file error.C.

Referenced by motionSmootherAlgo::checkMesh().

Here is the caller graph for this function:

◆ clear()

void clear ( ) const

Clear any messages.

Definition at line 325 of file error.C.

Referenced by motionSmootherAlgo::checkMesh().

Here is the caller graph for this function:

◆ functionName()

const string & functionName ( ) const
inlinenoexcept

The currently defined function name for output messages.

Definition at line 151 of file error.H.

References error::functionName_.

◆ sourceFileName()

const string & sourceFileName ( ) const
inlinenoexcept

The currently defined source-file name for output messages.

Definition at line 157 of file error.H.

References error::sourceFileName_.

◆ sourceFileLineNumber()

label sourceFileLineNumber ( ) const
inlinenoexcept

The currently defined source-file line number for output messages.

Definition at line 163 of file error.H.

References error::sourceFileLineNumber_.

◆ throwing() [1/2]

bool throwing ( ) const
inlinenoexcept

Return the current exception throwing state (on or off)

Definition at line 169 of file error.H.

References error::throwing_.

Referenced by functionObjectList::end(), functionObjectList::execute(), if(), IOobjectList::IOobjectList(), and functionObjectList::read().

Here is the caller graph for this function:

◆ throwing() [2/2]

bool throwing ( const bool  on)
inlinenoexcept

Specify exception throwing state (on or off)

Returns
the previous throwing state

Definition at line 176 of file error.H.

◆ stream()

Foam::OSstream & stream ( )

Return OSstream for output operations.

Definition at line 304 of file error.C.

References Foam::abort(), Foam::endl(), Foam::nl, and Foam::Perr.

Referenced by error::operator OSstream &(), error::operator()(), and Time::readDict().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator OSstream &()

operator OSstream & ( )
inline

Implicit cast to OSstream for << operations.

Definition at line 190 of file error.H.

References error::stream().

Here is the call graph for this function:

◆ operator()() [1/4]

OSstream & operator() ( )
inline

Explicit convert to OSstream for << operations.

Definition at line 196 of file error.H.

References error::stream().

Here is the call graph for this function:

◆ operator()() [2/4]

Foam::OSstream & operator() ( const string functionName)

Define basic print message.

Returns
OSstream for further operations

Definition at line 151 of file error.C.

◆ operator()() [3/4]

Foam::OSstream & operator() ( const char *  functionName,
const char *  sourceFileName,
const int  sourceFileLineNumber = 0 
)

Define basic print message.

Returns
OSstream for further operations

Definition at line 164 of file error.C.

◆ operator()() [4/4]

Foam::OSstream & operator() ( const string functionName,
const char *  sourceFileName,
const int  sourceFileLineNumber = 0 
)

Define basic print message.

Returns
OSstream for further operations

Definition at line 190 of file error.C.

◆ operator dictionary()

operator dictionary ( ) const

Extract a dictionary representation of the error information.

◆ safePrintStack()

void safePrintStack ( std::ostream &  os)
static

Helper function to print a stack, used when OpenFOAM IO not yet initialised.

Definition at line 32 of file dummyPrintStack.C.

◆ printStack()

void printStack ( Ostream os)
static

Helper function to print a stack.

Definition at line 36 of file dummyPrintStack.C.

Referenced by Foam::exitNow(), messageStream::masterStream(), GeometricField< Type, PatchField, GeoMesh >::oldTime(), Foam::reduce(), and error::simpleExit().

Here is the caller graph for this function:

◆ useAbort()

bool useAbort ( )
static

True if FOAM_ABORT is on.

Definition at line 98 of file error.C.

References Switch::find(), and Foam::getEnv().

Referenced by Foam::exitNow(), and error::simpleExit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ exit()

void exit ( const int  errNo = 1)

Exit : can be called for any error to exit program.

Redirects to abort() when FOAM_ABORT is on.

Definition at line 331 of file error.C.

Referenced by if().

Here is the caller graph for this function:

◆ abort()

void abort ( )

Abort : used to stop code for fatal errors.

Prints stack before exiting.

Definition at line 337 of file error.C.

◆ write()

void write ( Ostream os,
const bool  withTitle = true 
) const
virtual

Print error message.

Reimplemented in IOerror.

Definition at line 343 of file error.C.

References Foam::foamVersion::api, IOstream::bad(), messageStream::level, Foam::nl, os(), Foam::foamVersion::patch, and Foam::foamVersion::patched().

Referenced by functionObjectList::end(), functionObjectList::execute(), Foam::operator<<(), argList::parse(), and functionObjectList::read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ throwExceptions()

bool throwExceptions ( const bool  on = true)
inlinenoexcept

Specify exception throwing state (default is on)

Returns
the previous throwing state

Definition at line 260 of file error.H.

◆ dontThrowExceptions()

bool dontThrowExceptions ( )
inlinenoexcept

Deactivate exception throwing.

Returns
the previous throwing state

Definition at line 267 of file error.H.

Member Data Documentation

◆ functionName_

string functionName_
protected

Definition at line 88 of file error.H.

Referenced by error::functionName().

◆ sourceFileName_

string sourceFileName_
protected

Definition at line 89 of file error.H.

Referenced by error::sourceFileName().

◆ sourceFileLineNumber_

label sourceFileLineNumber_
protected

Definition at line 90 of file error.H.

Referenced by error::sourceFileLineNumber().

◆ throwing_

bool throwing_
protected

Definition at line 91 of file error.H.

Referenced by error::throwing().

◆ messageStreamPtr_

std::unique_ptr<OStringStream> messageStreamPtr_
protected

Definition at line 92 of file error.H.


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