diff --git a/applications/test/faces/Test-faces.C b/applications/test/faces/Test-faces.C index 823f33e3e1..280cdf7006 100644 --- a/applications/test/faces/Test-faces.C +++ b/applications/test/faces/Test-faces.C @@ -52,7 +52,7 @@ int main(int argc, char *argv[]) Info<< "face:" << f1 << nl; // expect these to fail - FatalError.throwExceptions(); + const bool throwingError = FatalError.throwExceptions(); try { labelledTri l1{ 1, 2, 3, 10, 24 }; @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) WarningInFunction << "Caught FatalError " << err << nl << endl; } - FatalError.dontThrowExceptions(); + FatalError.throwExceptions(throwingError); labelledTri l2{ 1, 2, 3 }; Info<< "labelled:" << l2 << nl; diff --git a/applications/utilities/postProcessing/postProcess/postProcess.C b/applications/utilities/postProcessing/postProcess/postProcess.C index 3f7f51dd2d..8a01c21eb4 100644 --- a/applications/utilities/postProcessing/postProcess/postProcess.C +++ b/applications/utilities/postProcessing/postProcess/postProcess.C @@ -191,7 +191,7 @@ int main(int argc, char *argv[]) ); } - FatalIOError.throwExceptions(); + const bool throwingIOErr = FatalIOError.throwExceptions(); try { @@ -208,12 +208,15 @@ int main(int argc, char *argv[]) // Report to output (avoid overwriting values from simulation) profiling::print(Info); } - catch (IOerror& err) + catch (Foam::IOerror& err) { Warning<< err << endl; } Info<< endl; + + // Restore previous exception throwing state + FatalIOError.throwExceptions(throwingIOErr); } Info<< "End\n" << endl; diff --git a/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C b/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C index 7d639e7b5c..3147e59380 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C +++ b/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C @@ -65,7 +65,7 @@ bool Foam::IOobject::readHeader(Istream& is) && firstToken.wordToken() == "FoamFile" ) { - dictionary headerDict(is); + const dictionary headerDict(is); is.version(headerDict.lookup("version")); is.format(headerDict.lookup("format")); diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index 4e2fab2e0e..4ddaa2473f 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -30,7 +30,7 @@ License #include "JobInfo.H" #include "Pstream.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::IOerror::IOerror(const string& title) : @@ -50,10 +50,14 @@ Foam::IOerror::IOerror(const dictionary& errDict) {} +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + Foam::IOerror::~IOerror() throw() {} +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + Foam::OSstream& Foam::IOerror::operator() ( const char* functionName, @@ -167,6 +171,8 @@ Foam::IOerror::operator Foam::dictionary() const } +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + void Foam::IOerror::exit(const int) { 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()) { - os << endl - << ioErr.title().c_str() << endl - << ioErr.message().c_str() << endl << endl; + os << nl + << err.title().c_str() << nl + << 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() - << " to line " << ioErr.ioEndLineNumber() << '.'; + os << " from line " << err.ioStartLineNumber() + << " 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 - << " From function " << ioErr.functionName().c_str() << endl - << " in file " << ioErr.sourceFileName().c_str() - << " at line " << ioErr.sourceFileLineNumber() << '.'; + os << nl << nl + << " From function " << err.functionName().c_str() << endl + << " in file " << err.sourceFileName().c_str() + << " 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: "); + // ************************************************************************* // diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index e0dbff97d8..cc0907d222 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -31,7 +31,8 @@ License #include "Pstream.H" #include "OSspecific.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::error::error(const string& title) : @@ -88,12 +89,16 @@ Foam::error::error(const error& err) } +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + Foam::error::~error() throw() { delete messageStreamPtr_; } +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + Foam::OSstream& Foam::error::operator() ( const char* functionName, @@ -156,6 +161,8 @@ Foam::error::operator Foam::dictionary() const } +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + Foam::string Foam::error::message() const { return messageStreamPtr_->str(); @@ -249,18 +256,20 @@ void Foam::error::abort() } -Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr) -{ - os << endl - << fErr.title().c_str() << endl - << fErr.message().c_str(); +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - 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 - << " From function " << fErr.functionName().c_str() << endl - << " in file " << fErr.sourceFileName().c_str() - << " at line " << fErr.sourceFileLineNumber() << '.'; + os << nl << nl + << " From function " << err.functionName().c_str() << endl + << " in file " << err.sourceFileName().c_str() + << " at line " << err.sourceFileLineNumber() << '.'; } return os; @@ -272,4 +281,5 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr) Foam::error Foam::FatalError("--> FOAM FATAL ERROR: "); + // ************************************************************************* // diff --git a/src/OpenFOAM/db/error/error.H b/src/OpenFOAM/db/error/error.H index df42bf1ce0..961bd3cd4d 100644 --- a/src/OpenFOAM/db/error/error.H +++ b/src/OpenFOAM/db/error/error.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -30,7 +30,7 @@ Description 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 which may + 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). @@ -57,7 +57,7 @@ namespace Foam // Forward declaration of friend functions and operators class error; -Ostream& operator<<(Ostream&, const error&); +Ostream& operator<<(Ostream& os, const error& err); /*---------------------------------------------------------------------------*\ @@ -90,10 +90,10 @@ public: error(const string& title); //- Construct from dictionary - error(const dictionary&); + error(const dictionary& errDict); //- Construct as copy - error(const error&); + error(const error& err); //- Destructor @@ -104,29 +104,48 @@ public: string message() const; - const string& functionName() const + inline const string& functionName() const { return functionName_; } - const string& sourceFileName() const + inline const string& sourceFileName() const { return sourceFileName_; } - label sourceFileLineNumber() const + inline label sourceFileLineNumber() const { 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 @@ -157,16 +176,16 @@ public: return operator OSstream&(); } - //- Create and return a dictionary + //- Create and return a dictionary representation of the error operator dictionary() const; //- Helper function to print a stack (if OpenFOAM IO not yet // initialised) - static void safePrintStack(std::ostream&); + static void safePrintStack(std::ostream& os); //- 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. // Prints stack before exiting. @@ -179,13 +198,13 @@ public: // Ostream operator - friend Ostream& operator<<(Ostream&, const error&); + friend Ostream& operator<<(Ostream& os, const error& err); }; // Forward declaration of friend functions and operators class IOerror; -Ostream& operator<<(Ostream&, const IOerror&); +Ostream& operator<<(Ostream& os, const IOerror& err); /*---------------------------------------------------------------------------*\ @@ -212,7 +231,7 @@ public: IOerror(const string& title); //- Construct from dictionary - IOerror(const dictionary&); + IOerror(const dictionary& errDict); //- Destructor @@ -221,17 +240,17 @@ public: // Member functions - const string& ioFileName() const + inline const string& ioFileName() const { return ioFileName_; } - label ioStartLineNumber() const + inline label ioStartLineNumber() const { return ioStartLineNumber_; } - label ioEndLineNumber() const + inline label ioEndLineNumber() const { return ioEndLineNumber_; } @@ -279,7 +298,7 @@ public: const string& msg ); - //- Create and return a dictionary + //- Create and return a dictionary representation of the error operator dictionary() const; @@ -292,7 +311,7 @@ public: // Ostream operator - friend Ostream& operator<<(Ostream&, const IOerror&); + friend Ostream& operator<<(Ostream& os, const IOerror& err); }; diff --git a/src/OpenFOAM/db/error/messageStream.C b/src/OpenFOAM/db/error/messageStream.C index 661d249602..c0b9e7219b 100644 --- a/src/OpenFOAM/db/error/messageStream.C +++ b/src/OpenFOAM/db/error/messageStream.C @@ -90,8 +90,8 @@ Foam::OSstream& Foam::messageStream::operator() { OSstream& os = operator OSstream&(); - os << endl - << " From function " << functionName << endl + os << nl + << " From function " << functionName << nl << " in file " << sourceFileName << " at line " << sourceFileLineNumber << endl << " "; @@ -194,7 +194,7 @@ Foam::messageStream::operator Foam::OSstream&() { if (level) { - bool collect = (severity_ == INFO || severity_ == WARNING); + const bool collect = (severity_ == INFO || severity_ == WARNING); // Report the error if (!Pstream::master() && collect) diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index 37db0a3067..9f6070472a 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -752,8 +752,10 @@ bool Foam::functionObjectList::read() { autoPtr foPtr; - FatalError.throwExceptions(); - FatalIOError.throwExceptions(); + // Throw FatalError, FatalIOError as exceptions + const bool throwingError = FatalError.throwExceptions(); + const bool throwingIOerr = FatalIOError.throwExceptions(); + try { // New functionObject @@ -784,8 +786,10 @@ bool Foam::functionObjectList::read() WarningInFunction << "Caught FatalError " << err << nl << endl; } - FatalError.dontThrowExceptions(); - FatalIOError.dontThrowExceptions(); + + // Restore previous exception throwing state + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOerr); if (foPtr.valid()) {