1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
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 //- Create and return a dictionary
269 operator dictionary() const;
272 //- Exit : can be called for any error to exit program
273 void exit(const int errNo = 1);
275 //- Abort : used to stop code for fatal errors
281 friend Ostream& operator<<(Ostream&, const IOerror&);
285 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
286 // Global error declarations: defined in error.C
288 extern error FatalError;
289 extern IOerror FatalIOError;
291 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
293 } // End namespace Foam
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 // Convenience macros to add the file name and line number to the function name
299 * \def FatalErrorIn(functionName)
300 * Report an error message using Foam::FatalError for functionName in
301 * file __FILE__ at line __LINE__
303 #define FatalErrorIn(fn) \
304 ::Foam::FatalError((fn), __FILE__, __LINE__)
307 * \def FatalIOErrorIn(functionName, ios)
308 * Report an error message using Foam::FatalIOError for functionName in
309 * file __FILE__ at line __LINE__
310 * for a particular IOstream
312 #define FatalIOErrorIn(fn, ios) \
313 ::Foam::FatalIOError((fn), __FILE__, __LINE__, (ios))
316 * \def notImplemented(functionName)
317 * Issue a FatalErrorIn for the functionName.
318 * This is used for functions that are not currently implemented.
319 * The functionName is printed and then abort is called.
322 * This macro can be particularly useful when methods must be defined to
323 * complete the interface of a derived class even if they should never be
324 * called for this derived class.
326 #define notImplemented(fn) \
327 FatalErrorIn(fn) << "Not implemented" << ::Foam::abort(FatalError);
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331 #include "errorManip.H"
333 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
337 // ************************************************************************* //