1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
28 Class to handle errors and exceptions in a simple, consistent stream-based
31 The error class is globaly instantiated with a title string. Errors,
32 messages and other data are piped to the messageStream class in the
33 standard manner. Manipulators are supplied for exit and abort which may
34 terminate the program or throw an exception depending of if the exception
35 handling has beed switched on (off by default).
39 error << "message1" << "message2" << FoamDataType << exit(errNo);
40 error << "message1" << "message2" << FoamDataType << abort();
46 \*---------------------------------------------------------------------------*/
51 #include "messageStream.H"
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 // Forward declaration of friend functions and operators
60 Ostream& operator<<(Ostream&, const error&);
63 /*---------------------------------------------------------------------------*\
64 Class error Declaration
65 \*---------------------------------------------------------------------------*/
69 public std::exception,
78 string sourceFileName_;
79 label sourceFileLineNumber_;
83 bool throwExceptions_;
84 OStringStream* messageStreamPtr_;
90 //- Construct from title string
91 error(const string& title);
93 //- Construct from dictionary
94 error(const dictionary&);
101 virtual ~error() throw();
106 string message() const;
108 const string& functionName() const
110 return functionName_;
113 const string& sourceFileName() const
115 return sourceFileName_;
118 label sourceFileLineNumber() const
120 return sourceFileLineNumber_;
123 void throwExceptions()
125 throwExceptions_ = true;
128 void dontThrowExceptions()
130 throwExceptions_ = false;
133 //- Convert to OSstream
134 // Prints basic message and returns OSstream for further info.
137 const char* functionName,
138 const char* sourceFileName,
139 const int sourceFileLineNumber = 0
142 //- Convert to OSstream
143 // Prints basic message and returns OSstream for further info.
146 const string& functionName,
147 const char* sourceFileName,
148 const int sourceFileLineNumber = 0
151 //- Convert to OSstream
152 // Prints basic message and returns OSstream for further info.
153 operator OSstream&();
155 //- Explicitly convert to OSstream for << operations
156 OSstream& operator()()
158 return operator OSstream&();
161 //- Create and return a dictionary
162 operator dictionary() const;
165 //- Helper function to print a stack
166 static void printStack(Ostream&);
168 //- Exit : can be called for any error to exit program.
169 // Prints stack before exiting.
170 void exit(const int errNo = 1);
172 //- Abort : used to stop code for fatal errors.
173 // Prints stack before exiting.
179 friend Ostream& operator<<(Ostream&, const error&);
183 // Forward declaration of friend functions and operators
185 Ostream& operator<<(Ostream&, const IOerror&);
188 /*---------------------------------------------------------------------------*\
189 Class IOerror Declaration
190 \*---------------------------------------------------------------------------*/
192 //- Report an I/O error
200 label ioStartLineNumber_;
201 label ioEndLineNumber_;
208 //- Construct from title string
209 IOerror(const string& title);
211 //- Construct from dictionary
212 IOerror(const dictionary&);
216 virtual ~IOerror() throw();
221 const string& ioFileName() const
226 label ioStartLineNumber() const
228 return ioStartLineNumber_;
231 label ioEndLineNumber() const
233 return ioEndLineNumber_;
236 //- Convert to OSstream
237 // Prints basic message and returns OSstream for further info.
240 const char* functionName,
241 const char* sourceFileName,
242 const int sourceFileLineNumber,
243 const string& ioFileName,
244 const label ioStartLineNumber = -1,
245 const label ioEndLineNumber = -1
248 //- Convert to OSstream
249 // Prints basic message and returns OSstream for further info.
252 const char* functionName,
253 const char* sourceFileName,
254 const int sourceFileLineNumber,
258 //- Convert to OSstream
259 // Prints basic message and returns OSstream for further info.
262 const char* functionName,
263 const char* sourceFileName,
264 const int sourceFileLineNumber,
268 //- Print basic message and exit. Uses cerr if streams not constructed
269 // yet (at startup). Use in startup parsing instead of FatalError.
270 static void SafeFatalIOError
272 const char* functionName,
273 const char* sourceFileName,
274 const int sourceFileLineNumber,
279 //- Create and return a dictionary
280 operator dictionary() const;
283 //- Exit : can be called for any error to exit program
284 void exit(const int errNo = 1);
286 //- Abort : used to stop code for fatal errors
292 friend Ostream& operator<<(Ostream&, const IOerror&);
296 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
297 // Global error declarations: defined in error.C
299 extern error FatalError;
300 extern IOerror FatalIOError;
302 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
304 } // End namespace Foam
306 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
307 // Convenience macros to add the file name and line number to the function name
310 * \def FatalErrorIn(functionName)
311 * Report an error message using Foam::FatalError for functionName in
312 * file __FILE__ at line __LINE__
314 #define FatalErrorIn(fn) \
315 ::Foam::FatalError((fn), __FILE__, __LINE__)
318 * \def FatalIOErrorIn(functionName, ios)
319 * Report an error message using Foam::FatalIOError for functionName in
320 * file __FILE__ at line __LINE__
321 * for a particular IOstream
323 #define FatalIOErrorIn(fn, ios) \
324 ::Foam::FatalIOError((fn), __FILE__, __LINE__, (ios))
327 * \def SafeFatalIOErrorIn(functionName, ios, msg)
328 * Report an error message using Foam::FatalIOError (or cerr if FatalIOError
329 * not yet constructed) for functionName in
330 * file __FILE__ at line __LINE__
331 * for a particular IOstream
333 #define SafeFatalIOErrorIn(fn, ios, msg) \
334 ::Foam::IOerror::SafeFatalIOError((fn), __FILE__, __LINE__, (ios), (msg))
337 * \def notImplemented(functionName)
338 * Issue a FatalErrorIn for the functionName.
339 * This is used for functions that are not currently implemented.
340 * The functionName is printed and then abort is called.
343 * This macro can be particularly useful when methods must be defined to
344 * complete the interface of a derived class even if they should never be
345 * called for this derived class.
347 #define notImplemented(fn) \
348 FatalErrorIn(fn) << "Not implemented" << ::Foam::abort(FatalError);
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 #include "errorManip.H"
354 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 // ************************************************************************* //