error.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2015-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::error
29 
30 Description
31  Class to handle errors and exceptions in a simple, consistent stream-based
32  manner.
33 
34  The error class is globally instantiated with a title string. Errors,
35  messages and other data are piped to the messageStream class in the
36  standard manner. Manipulators are supplied for exit and abort that may
37  terminate the program or throw an exception depending on whether the
38  exception handling has been switched on (off by default).
39 
40 Usage
41  \code
42  error << "message1" << "message2" << FoamDataType << exit(errNo);
43  error << "message1" << "message2" << FoamDataType << abort();
44  \endcode
45 
46 SourceFiles
47  error.C
48  IOerror.C
49 
50 SeeAlso
51  Foam::errorManip
52  Foam::errorManipArg
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #ifndef Foam_error_H
57 #define Foam_error_H
58 
59 #include "messageStream.H"
60 #include <memory>
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 namespace Foam
65 {
66 
67 // Forward Declarations
68 class OStringStream;
69 template<class EnumType> class Enum;
70 
71 /*---------------------------------------------------------------------------*\
72  Class error Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 class error
76 :
77  public std::exception,
78  public messageStream
79 {
80  // Private Member Functions
81 
82  //- Exit or abort code, with exception and jobinfo handling
83  void exiting(const int errNo, const bool isAbort);
84 
85 
86 protected:
87 
88  // Protected Data
89 
90  string functionName_;
93  bool throwing_;
94  std::unique_ptr<OStringStream> messageStreamPtr_;
95 
96 
97  // Protected Member Functions
98 
99  //- Exit or abort, without throwing or job control handling
100  void simpleExit(const int errNo, const bool isAbort);
101 
102 
103 public:
104 
105  // Data Types
106 
107  //- Handling of errors. The exact handling depends on the local context.
108  enum class handlerTypes : char
109  {
110  DEFAULT = 0,
111  IGNORE,
112  WARN,
113  STRICT
114  };
115 
116  //- Names of the error handler types
117  static const Enum<handlerTypes> handlerNames;
118 
119 
120  // Constructors
121 
122  //- Construct from title string
123  explicit error(const char* title);
124 
125  //- Construct from title string
126  explicit error(const std::string& title) : error(title.c_str()) {}
127 
128  //- Construct from dictionary
129  explicit error(const dictionary& errDict);
130 
131  //- Copy construct
132  error(const error& err);
134 
135  //- Destructor
136  virtual ~error() noexcept;
137 
138 
139  // Static Functions
140 
141  //- Like Pstream::master but with a Pstream::parRun guard in case
142  //- Pstream has not yet been initialised.
143  //
144  // \param communicator is the numbered MPI communicator.
145  // By default it uses UPstream::worldComm
146  static bool master(const int communicator = -1);
147 
148  //- Test if an age warning should be emitted.
149  // \param version is the old version (YYMM) for determining the
150  // age in months compared to the current OpenFOAM version
151  // as conveyed by the \c foamVersion::api value.
152  static bool warnAboutAge(const int version) noexcept;
153 
154  //- Emit warning on stderr about something being old.
155  // \param what description for the warning
156  // \param version is the old version (YYMM) for determining the
157  // age in months compared to the current OpenFOAM version
158  // as conveyed by the \c foamVersion::api value.
159  static bool warnAboutAge(const char* what, const int version);
160 
161 
162  // Member Functions
163 
164  //- The accumulated error message
165  string message() const;
166 
167  //- Clear any accumulated error messages
168  void clear() const;
169 
170  //- The currently defined function name for output messages
171  const string& functionName() const noexcept
172  {
173  return functionName_;
174  }
175 
176  //- The currently defined source-file name for output messages
177  const string& sourceFileName() const noexcept
178  {
179  return sourceFileName_;
180  }
181 
182  //- The currently defined source-file line number for output messages
183  int sourceFileLineNumber() const noexcept
184  {
185  return sourceFileLineNumber_;
186  }
187 
188  //- Return the current exception throwing state (on or off)
189  bool throwing() const noexcept
190  {
191  return throwing_;
192  }
193 
194  //- Specify exception throwing state (on or off)
195  // \return the previous throwing state
196  bool throwing(const bool on) noexcept
197  {
198  bool old(throwing_);
199  throwing_ = on;
200  return old;
201  }
202 
203 
204  // Output
205 
206  //- Return OSstream for output operations
208 
209  //- Implicit cast to OSstream for << operations
210  operator OSstream&()
211  {
212  return this->stream();
213  }
214 
215  //- Explicit convert to OSstream for << operations
217  {
218  return this->stream();
219  }
220 
221  //- Define basic print message with originating function name,
222  //- optionally with 'source file, line number'
223  // \return OSstream for further operations
224  OSstream& operator()
225  (
226  string functionName,
227  const char* sourceFileName = nullptr,
228  const int sourceFileLineNumber = -1
229  );
230 
231  //- Define basic print message with originating function name,
232  //- optionally with 'source file, line number'
233  // \return OSstream for further operations
234  OSstream& operator()
235  (
236  const char* functionName,
237  const char* sourceFileName = nullptr,
238  const int sourceFileLineNumber = -1
239  );
240 
241 
242  // Other
243 
244  //- Extract a dictionary representation of the error information
245  operator dictionary() const;
246 
247 
248  //- Helper function to print a stack, with optional upper limit.
249  //- Used when OpenFOAM IO not yet initialised.
250  static void safePrintStack(std::ostream& os, int size = -1);
252  //- Helper function to print a stack, with optional upper limit.
253  static void printStack(Ostream& os, int size = -1);
254 
255  //- True if FOAM_ABORT is on.
256  static bool useAbort();
257 
258 
259  //- Exit : can be called for any error to exit program.
260  // Redirects to abort() when FOAM_ABORT is on.
261  void exit(const int errNo = 1);
262 
263  //- Abort : used to stop code for fatal errors.
264  // Prints stack before exiting.
265  void abort();
266 
267  //- Print error message
268  virtual void write(Ostream& os, const bool withTitle = true) const;
269 
270 
271  // Housekeeping
272 
273  //- Specify exception throwing state (default is \b on)
274  // \return the previous throwing state
275  bool throwExceptions(const bool on = true) noexcept
276  {
277  return throwing(on);
278  }
279 
280  //- Deactivate exception throwing
281  // \return the previous throwing state
283  {
284  return throwExceptions(false);
285  }
286 };
287 
288 
289 /*---------------------------------------------------------------------------*\
290  Class IOerror Declaration
291 \*---------------------------------------------------------------------------*/
292 
293 //- Report an I/O error
294 class IOerror
295 :
296  public error
297 {
298  // Private Data
299 
300  string ioFileName_;
301  label ioStartLineNumber_;
302  label ioEndLineNumber_;
303 
304 
305  // Private Member Functions
306 
307  //- Exit or abort code, with exception and jobinfo handling
308  void exiting(const int errNo, const bool isAbort);
309 
310 
311 public:
312 
313  // Constructors
314 
315  //- Construct from title string
316  explicit IOerror(const char* title);
317 
318  //- Construct from title string
319  explicit IOerror(const std::string& title) : IOerror(title.c_str()) {}
320 
321  //- Construct from dictionary
322  explicit IOerror(const dictionary& errDict);
323 
324 
325  //- Destructor
326  virtual ~IOerror() noexcept;
327 
328 
329  // Member Functions
330 
331  //- The currently defined IO name for output messages
332  const string& ioFileName() const noexcept
333  {
334  return ioFileName_;
335  }
336 
337  //- The currently defined IO start-line number for output messages
338  label ioStartLineNumber() const noexcept
339  {
340  return ioStartLineNumber_;
341  }
342 
343  //- The currently defined IO end-line number
344  label ioEndLineNumber() const noexcept
345  {
346  return ioEndLineNumber_;
347  }
348 
349 
350  // Output
351 
352  //- Define basic print message
353  // \return OSstream for further operations
354  OSstream& operator()
355  (
356  const char* functionName,
357  const char* sourceFileName,
358  const int sourceFileLineNumber,
359  string ioFileName,
360  const label ioStartLineNumber = -1,
361  const label ioEndLineNumber = -1
362  );
363 
364  //- Define basic print message for IO stream
365  // \return OSstream for further operations
366  OSstream& operator()
367  (
368  const char* functionName,
369  const char* sourceFileName,
370  const int sourceFileLineNumber,
371  const IOstream& ioStream
372  );
373 
374  //- Define basic print message for dictionary entries.
375  //- Uses the dictionary::relativeName() on output.
376  // \return OSstream for further operations
377  OSstream& operator()
378  (
379  const char* functionName,
380  const char* sourceFileName,
381  const int sourceFileLineNumber,
382  const dictionary& dict
383  );
384 
385 
386  //- Define basic print message for IO stream,
387  //- independent of a source-file reference.
388  //- Uses the dictionary::relativeName() on output.
389  // \return OSstream for further operations
390  OSstream& operator()
391  (
392  const std::string& where,
393  const IOstream& ioStream
394  );
395 
396  //- Define basic print message for dictionary entries,
397  //- independent of a source-file reference.
398  //- Uses the dictionary::relativeName() on output.
399  // \return OSstream for further operations
400  OSstream& operator()
401  (
402  const std::string& where,
403  const dictionary& dict
404  );
405 
406 
407  //- Print basic message and exit.
408  // Uses cerr if streams not yet constructed (at startup).
409  // Use in startup parsing instead of FatalError.
410  static void SafeFatalIOError
411  (
412  const char* functionName,
413  const char* sourceFileName,
414  const int sourceFileLineNumber,
415  const IOstream& ioStream,
416  const std::string& msg
417  );
418 
419 
420  // Other
421 
422  //- Extract a dictionary representation of the error information
423  operator dictionary() const;
424 
425  //- Exit : can be called for any error to exit program
426  void exit(const int errNo = 1);
427 
428  //- Abort : used to stop code for fatal errors
429  void abort();
430 
431  //- Print error message
432  virtual void write(Ostream& os, const bool withTitle = true) const;
433 };
434 
435 
436 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
437 
438 //- Ostream operator
439 Ostream& operator<<(Ostream& os, const error& err);
440 
441 //- Ostream operator
442 Ostream& operator<<(Ostream& os, const IOerror& err);
443 
444 
445 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
446 // Global error declarations: defined in error.C
447 
448 //- Error stream (stdout output on all processes),
449 //- with additional 'FOAM FATAL ERROR' header text and stack trace.
450 extern error FatalError;
451 
452 //- Error stream (stdout output on all processes),
453 //- with additional 'FOAM FATAL IO ERROR' header text and stack trace.
454 extern IOerror FatalIOError;
455 
456 
457 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
458 
459 } // End namespace Foam
460 
461 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
462 // Convenience macros to add the file name and line number to the function name
463 
464 //- Report an error message using Foam::FatalError
465 // for functionName in file __FILE__ at line __LINE__
466 #define FatalErrorIn(functionName) \
467  ::Foam::FatalError((functionName), __FILE__, __LINE__)
468 
469 //- Report an error message using Foam::FatalError
470 // for FUNCTION_NAME in file __FILE__ at line __LINE__
471 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
472 
473 //- Report an error message using Foam::FatalError
474 // about unknown lookup type in table
475 #define FatalErrorInLookup(lookupTag, lookupName, lookupTable) \
476  ::Foam::FatalError(FUNCTION_NAME, __FILE__, __LINE__) \
477  << "Unknown " << (lookupTag) << " type " << (lookupName) \
478  << "\n\nValid " << (lookupTag) << " types :\n" \
479  << ((lookupTable).sortedToc()) << '\n'
480 
481 
482 //- Report an error message using Foam::FatalIOError
483 // for functionName in file __FILE__ at line __LINE__
484 // for a particular IOstream
485 #define FatalIOErrorIn(functionName, ios) \
486  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
487 
488 //- Report an error message using Foam::FatalIOError
489 // for FUNCTION_NAME in file __FILE__ at line __LINE__
490 // for a particular IOstream
491 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
492 
493 
494 //- Report an error message using Foam::FatalIOError
495 // about unknown lookup type in table
496 #define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable) \
497  ::Foam::FatalIOError(FUNCTION_NAME, __FILE__, __LINE__, (ios)) \
498  << "Unknown " << (lookupTag) << " type " << (lookupName) \
499  << "\n\nValid " << (lookupTag) << " types :\n" \
500  << ((lookupTable).sortedToc()) << '\n'
501 
502 
503 //- Report an error message using Foam::FatalIOError
504 // (or cerr if FatalIOError not yet constructed)
505 // for functionName in file __FILE__ at line __LINE__
506 // for a particular IOstream
507 #define SafeFatalIOErrorIn(functionName, ios, msg) \
508  ::Foam::IOerror::SafeFatalIOError \
509  ((functionName), __FILE__, __LINE__, (ios), (msg))
510 
511 //- Report an error message using Foam::FatalIOError
512 // (or cerr if FatalIOError not yet constructed)
513 // for functionName in file __FILE__ at line __LINE__
514 // for a particular IOstream
515 #define SafeFatalIOErrorInFunction(ios, msg) \
516  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
517 
518 
519 //- Issue a FatalErrorIn for a function not currently implemented.
520 // The functionName is printed and then abort is called.
521 //
522 // This macro can be particularly useful when methods must be defined to
523 // complete the interface of a derived class even if they should never be
524 // called for this derived class.
525 #define notImplemented(functionName) \
526  FatalErrorIn(functionName) \
527  << "Not implemented" << ::Foam::abort(FatalError);
528 
529 //- Issue a FatalErrorIn for a function not currently implemented.
530 // The FUNCTION_NAME is printed and then abort is called.
531 //
532 // This macro can be particularly useful when methods must be defined to
533 // complete the interface of a derived class even if they should never be
534 // called for this derived class.
535 #define NotImplemented notImplemented(FUNCTION_NAME)
536 
537 
538 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
539 
540 #include "errorManip.H"
541 
542 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
543 
544 #endif
545 
546 // ************************************************************************* //
const string & title() const noexcept
The title of this error type.
Warn on errors/problems.
IOerror(const char *title)
Construct from title string.
Definition: IOerror.C:34
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
dictionary dict
void clear() const
Clear any accumulated error messages.
Definition: error.C:343
std::unique_ptr< OStringStream > messageStreamPtr_
Definition: error.H:91
handlerTypes
Handling of errors. The exact handling depends on the local context.
Definition: error.H:109
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:246
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual ~IOerror() noexcept
Destructor.
Definition: IOerror.C:54
bool throwing() const noexcept
Return the current exception throwing state (on or off)
Definition: error.H:223
bool dontThrowExceptions() noexcept
Deactivate exception throwing.
Definition: error.H:353
virtual ~error() noexcept
Destructor.
Definition: error.C:165
static bool master(const int communicator=-1)
Like Pstream::master but with a Pstream::parRun guard in case Pstream has not yet been initialised...
Definition: error.C:53
string sourceFileName_
Definition: error.H:88
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:70
Default behaviour (local meaning)
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:352
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:67
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:70
static void safePrintStack(std::ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit. Used when OpenFOAM IO not yet initialise...
static bool useAbort()
True if FOAM_ABORT is on.
Definition: error.C:110
const string & functionName() const noexcept
The currently defined function name for output messages.
Definition: error.H:199
string message() const
The accumulated error message.
Definition: error.C:332
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:358
virtual void write(Ostream &os, const bool withTitle=true) const
Print error message.
Definition: error.C:364
Ignore on errors/problems.
bool throwExceptions(const bool on=true) noexcept
Specify exception throwing state (default is on)
Definition: error.H:343
Report an I/O error.
Definition: error.H:367
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:240
const string & ioFileName() const noexcept
The currently defined IO name for output messages.
Definition: error.H:417
label ioStartLineNumber() const noexcept
The currently defined IO start-line number for output messages.
Definition: error.H:425
OBJstream os(runTime.globalPath()/outputName)
OSstream & operator()()
Explicit convert to OSstream for << operations.
Definition: error.H:259
string functionName_
Definition: error.H:87
virtual void write(Ostream &os, const bool withTitle=true) const
Print error message.
Definition: IOerror.C:252
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
bool throwing_
Definition: error.H:90
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
OSstream & stream()
Return OSstream for output operations.
Definition: error.C:314
int sourceFileLineNumber_
Definition: error.H:89
const string & sourceFileName() const noexcept
The currently defined source-file name for output messages.
Definition: error.H:207
label ioEndLineNumber() const noexcept
The currently defined IO end-line number.
Definition: error.H:433
static void printStack(Ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit.
int sourceFileLineNumber() const noexcept
The currently defined source-file line number for output messages.
Definition: error.H:215
static void SafeFatalIOError(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const IOstream &ioStream, const std::string &msg)
Print basic message and exit.
Definition: IOerror.C:162
Fatal on errors/problems.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:208
error(const char *title)
Construct from title string.
Definition: error.C:119
Namespace for OpenFOAM.
static const Enum< handlerTypes > handlerNames
Names of the error handler types.
Definition: error.H:120
void simpleExit(const int errNo, const bool isAbort)
Exit or abort, without throwing or job control handling.
Definition: error.C:263
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...