ENH: additional method for switching error throwing on/off (issue #552)

- error::throwExceptions(bool) returning the previous state makes it
  easier to set and restore states.

- throwing() method to query the current handling (if required).

- the normal error::throwExceptions() and error::dontThrowExceptions()
  also return the previous state, to make it easier to restore later.
This commit is contained in:
Mark Olesen
2017-07-29 12:25:30 +02:00
parent 41eafeb020
commit 797155f862
8 changed files with 105 additions and 62 deletions

View File

@ -52,7 +52,7 @@ int main(int argc, char *argv[])
Info<< "face:" << f1 << nl; Info<< "face:" << f1 << nl;
// expect these to fail // expect these to fail
FatalError.throwExceptions(); const bool throwingError = FatalError.throwExceptions();
try try
{ {
labelledTri l1{ 1, 2, 3, 10, 24 }; labelledTri l1{ 1, 2, 3, 10, 24 };
@ -63,7 +63,7 @@ int main(int argc, char *argv[])
WarningInFunction WarningInFunction
<< "Caught FatalError " << err << nl << endl; << "Caught FatalError " << err << nl << endl;
} }
FatalError.dontThrowExceptions(); FatalError.throwExceptions(throwingError);
labelledTri l2{ 1, 2, 3 }; labelledTri l2{ 1, 2, 3 };
Info<< "labelled:" << l2 << nl; Info<< "labelled:" << l2 << nl;

View File

@ -191,7 +191,7 @@ int main(int argc, char *argv[])
); );
} }
FatalIOError.throwExceptions(); const bool throwingIOErr = FatalIOError.throwExceptions();
try try
{ {
@ -208,12 +208,15 @@ int main(int argc, char *argv[])
// Report to output (avoid overwriting values from simulation) // Report to output (avoid overwriting values from simulation)
profiling::print(Info); profiling::print(Info);
} }
catch (IOerror& err) catch (Foam::IOerror& err)
{ {
Warning<< err << endl; Warning<< err << endl;
} }
Info<< endl; Info<< endl;
// Restore previous exception throwing state
FatalIOError.throwExceptions(throwingIOErr);
} }
Info<< "End\n" << endl; Info<< "End\n" << endl;

View File

@ -65,7 +65,7 @@ bool Foam::IOobject::readHeader(Istream& is)
&& firstToken.wordToken() == "FoamFile" && firstToken.wordToken() == "FoamFile"
) )
{ {
dictionary headerDict(is); const dictionary headerDict(is);
is.version(headerDict.lookup("version")); is.version(headerDict.lookup("version"));
is.format(headerDict.lookup("format")); is.format(headerDict.lookup("format"));

View File

@ -30,7 +30,7 @@ License
#include "JobInfo.H" #include "JobInfo.H"
#include "Pstream.H" #include "Pstream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::IOerror::IOerror(const string& title) Foam::IOerror::IOerror(const string& title)
: :
@ -50,10 +50,14 @@ Foam::IOerror::IOerror(const dictionary& errDict)
{} {}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::IOerror::~IOerror() throw() Foam::IOerror::~IOerror() throw()
{} {}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
Foam::OSstream& Foam::IOerror::operator() Foam::OSstream& Foam::IOerror::operator()
( (
const char* functionName, const char* functionName,
@ -167,6 +171,8 @@ Foam::IOerror::operator Foam::dictionary() const
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::IOerror::exit(const int) void Foam::IOerror::exit(const int)
{ {
if (!throwExceptions_ && JobInfo::constructed) if (!throwExceptions_ && JobInfo::constructed)
@ -254,32 +260,32 @@ void Foam::IOerror::abort()
} }
Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& ioErr) Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& err)
{ {
if (!os.bad()) if (!os.bad())
{ {
os << endl os << nl
<< ioErr.title().c_str() << endl << err.title().c_str() << nl
<< ioErr.message().c_str() << endl << endl; << err.message().c_str() << nl << endl;
os << "file: " << ioErr.ioFileName().c_str(); os << "file: " << err.ioFileName().c_str();
if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0) if (err.ioStartLineNumber() >= 0 && err.ioEndLineNumber() >= 0)
{ {
os << " from line " << ioErr.ioStartLineNumber() os << " from line " << err.ioStartLineNumber()
<< " to line " << ioErr.ioEndLineNumber() << '.'; << " to line " << err.ioEndLineNumber() << '.';
} }
else if (ioErr.ioStartLineNumber() >= 0) else if (err.ioStartLineNumber() >= 0)
{ {
os << " at line " << ioErr.ioStartLineNumber() << '.'; os << " at line " << err.ioStartLineNumber() << '.';
} }
if (IOerror::level >= 2 && ioErr.sourceFileLineNumber()) if (IOerror::level >= 2 && err.sourceFileLineNumber())
{ {
os << endl << endl os << nl << nl
<< " From function " << ioErr.functionName().c_str() << endl << " From function " << err.functionName().c_str() << endl
<< " in file " << ioErr.sourceFileName().c_str() << " in file " << err.sourceFileName().c_str()
<< " at line " << ioErr.sourceFileLineNumber() << '.'; << " at line " << err.sourceFileLineNumber() << '.';
} }
} }
@ -292,4 +298,5 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& ioErr)
Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: "); Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
// ************************************************************************* // // ************************************************************************* //

View File

@ -31,7 +31,8 @@ License
#include "Pstream.H" #include "Pstream.H"
#include "OSspecific.H" #include "OSspecific.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::error::error(const string& title) Foam::error::error(const string& title)
: :
@ -88,12 +89,16 @@ Foam::error::error(const error& err)
} }
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::error::~error() throw() Foam::error::~error() throw()
{ {
delete messageStreamPtr_; delete messageStreamPtr_;
} }
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
Foam::OSstream& Foam::error::operator() Foam::OSstream& Foam::error::operator()
( (
const char* functionName, const char* functionName,
@ -156,6 +161,8 @@ Foam::error::operator Foam::dictionary() const
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::string Foam::error::message() const Foam::string Foam::error::message() const
{ {
return messageStreamPtr_->str(); return messageStreamPtr_->str();
@ -249,18 +256,20 @@ void Foam::error::abort()
} }
Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr) // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
{
os << endl
<< fErr.title().c_str() << endl
<< fErr.message().c_str();
if (error::level >= 2 && fErr.sourceFileLineNumber()) Foam::Ostream& Foam::operator<<(Ostream& os, const error& err)
{
os << nl
<< err.title().c_str() << endl
<< err.message().c_str();
if (error::level >= 2 && err.sourceFileLineNumber())
{ {
os << endl << endl os << nl << nl
<< " From function " << fErr.functionName().c_str() << endl << " From function " << err.functionName().c_str() << endl
<< " in file " << fErr.sourceFileName().c_str() << " in file " << err.sourceFileName().c_str()
<< " at line " << fErr.sourceFileLineNumber() << '.'; << " at line " << err.sourceFileLineNumber() << '.';
} }
return os; return os;
@ -272,4 +281,5 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr)
Foam::error Foam::FatalError("--> FOAM FATAL ERROR: "); Foam::error Foam::FatalError("--> FOAM FATAL ERROR: ");
// ************************************************************************* // // ************************************************************************* //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -30,7 +30,7 @@ Description
The error class is globally instantiated with a title string. Errors, The error class is globally instantiated with a title string. Errors,
messages and other data are piped to the messageStream class in the messages and other data are piped to the messageStream class in the
standard manner. Manipulators are supplied for exit and abort which may standard manner. Manipulators are supplied for exit and abort that may
terminate the program or throw an exception depending on whether the terminate the program or throw an exception depending on whether the
exception handling has been switched on (off by default). exception handling has been switched on (off by default).
@ -57,7 +57,7 @@ namespace Foam
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
class error; class error;
Ostream& operator<<(Ostream&, const error&); Ostream& operator<<(Ostream& os, const error& err);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
@ -90,10 +90,10 @@ public:
error(const string& title); error(const string& title);
//- Construct from dictionary //- Construct from dictionary
error(const dictionary&); error(const dictionary& errDict);
//- Construct as copy //- Construct as copy
error(const error&); error(const error& err);
//- Destructor //- Destructor
@ -104,29 +104,48 @@ public:
string message() const; string message() const;
const string& functionName() const inline const string& functionName() const
{ {
return functionName_; return functionName_;
} }
const string& sourceFileName() const inline const string& sourceFileName() const
{ {
return sourceFileName_; return sourceFileName_;
} }
label sourceFileLineNumber() const inline label sourceFileLineNumber() const
{ {
return sourceFileLineNumber_; return sourceFileLineNumber_;
} }
void throwExceptions() //- Return the current exception throwing (on or off)
inline bool throwing() const
{ {
throwExceptions_ = true; return throwExceptions_;
} }
void dontThrowExceptions() //- Activate/deactivate exception throwing
// \return the previous throwing state
inline bool throwExceptions(bool doThrow)
{ {
throwExceptions_ = false; const bool prev = throwExceptions_;
throwExceptions_ = doThrow;
return prev;
}
//- Activate exception throwing
// \return the previous throwing state
inline bool throwExceptions()
{
return throwExceptions(true);
}
//- Deactivate exception throwing
// \return the previous throwing state
inline bool dontThrowExceptions()
{
return throwExceptions(false);
} }
//- Convert to OSstream //- Convert to OSstream
@ -157,16 +176,16 @@ public:
return operator OSstream&(); return operator OSstream&();
} }
//- Create and return a dictionary //- Create and return a dictionary representation of the error
operator dictionary() const; operator dictionary() const;
//- Helper function to print a stack (if OpenFOAM IO not yet //- Helper function to print a stack (if OpenFOAM IO not yet
// initialised) // initialised)
static void safePrintStack(std::ostream&); static void safePrintStack(std::ostream& os);
//- Helper function to print a stack //- Helper function to print a stack
static void printStack(Ostream&); static void printStack(Ostream& os);
//- Exit : can be called for any error to exit program. //- Exit : can be called for any error to exit program.
// Prints stack before exiting. // Prints stack before exiting.
@ -179,13 +198,13 @@ public:
// Ostream operator // Ostream operator
friend Ostream& operator<<(Ostream&, const error&); friend Ostream& operator<<(Ostream& os, const error& err);
}; };
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
class IOerror; class IOerror;
Ostream& operator<<(Ostream&, const IOerror&); Ostream& operator<<(Ostream& os, const IOerror& err);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
@ -212,7 +231,7 @@ public:
IOerror(const string& title); IOerror(const string& title);
//- Construct from dictionary //- Construct from dictionary
IOerror(const dictionary&); IOerror(const dictionary& errDict);
//- Destructor //- Destructor
@ -221,17 +240,17 @@ public:
// Member functions // Member functions
const string& ioFileName() const inline const string& ioFileName() const
{ {
return ioFileName_; return ioFileName_;
} }
label ioStartLineNumber() const inline label ioStartLineNumber() const
{ {
return ioStartLineNumber_; return ioStartLineNumber_;
} }
label ioEndLineNumber() const inline label ioEndLineNumber() const
{ {
return ioEndLineNumber_; return ioEndLineNumber_;
} }
@ -279,7 +298,7 @@ public:
const string& msg const string& msg
); );
//- Create and return a dictionary //- Create and return a dictionary representation of the error
operator dictionary() const; operator dictionary() const;
@ -292,7 +311,7 @@ public:
// Ostream operator // Ostream operator
friend Ostream& operator<<(Ostream&, const IOerror&); friend Ostream& operator<<(Ostream& os, const IOerror& err);
}; };

View File

@ -90,8 +90,8 @@ Foam::OSstream& Foam::messageStream::operator()
{ {
OSstream& os = operator OSstream&(); OSstream& os = operator OSstream&();
os << endl os << nl
<< " From function " << functionName << endl << " From function " << functionName << nl
<< " in file " << sourceFileName << " in file " << sourceFileName
<< " at line " << sourceFileLineNumber << endl << " at line " << sourceFileLineNumber << endl
<< " "; << " ";
@ -194,7 +194,7 @@ Foam::messageStream::operator Foam::OSstream&()
{ {
if (level) if (level)
{ {
bool collect = (severity_ == INFO || severity_ == WARNING); const bool collect = (severity_ == INFO || severity_ == WARNING);
// Report the error // Report the error
if (!Pstream::master() && collect) if (!Pstream::master() && collect)

View File

@ -752,8 +752,10 @@ bool Foam::functionObjectList::read()
{ {
autoPtr<functionObject> foPtr; autoPtr<functionObject> foPtr;
FatalError.throwExceptions(); // Throw FatalError, FatalIOError as exceptions
FatalIOError.throwExceptions(); const bool throwingError = FatalError.throwExceptions();
const bool throwingIOerr = FatalIOError.throwExceptions();
try try
{ {
// New functionObject // New functionObject
@ -784,8 +786,10 @@ bool Foam::functionObjectList::read()
WarningInFunction WarningInFunction
<< "Caught FatalError " << err << nl << endl; << "Caught FatalError " << err << nl << endl;
} }
FatalError.dontThrowExceptions();
FatalIOError.dontThrowExceptions(); // Restore previous exception throwing state
FatalError.throwExceptions(throwingError);
FatalIOError.throwExceptions(throwingIOerr);
if (foPtr.valid()) if (foPtr.valid())
{ {