1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 Class to handle errors and exceptions in a simple, consistent stream-based
32 The error class is globaly instantiated with a title string. Errors,
33 messages and other data are piped to the messageStream class in the
34 standard manner. Manipulators are supplied for exit and abort which may
35 terminate the program or throw an exception depending of if the exception
36 handling has beed switched on (off by default).
40 error << "message1" << "message2" << FoamDataType << exit(errNo);
41 error << "message1" << "message2" << FoamDataType << abort();
47 \*---------------------------------------------------------------------------*/
52 #include "messageStream.H"
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 // Forward declaration of friend functions and operators
61 Ostream& operator<<(Ostream&, const error&);
64 /*---------------------------------------------------------------------------*\
65 Class error Declaration
66 \*---------------------------------------------------------------------------*/
70 public std::exception,
79 string sourceFileName_;
80 label sourceFileLineNumber_;
84 bool throwExceptions_;
85 OStringStream* messageStreamPtr_;
91 //- Construct from title string
92 error(const string& title);
94 //- Construct from dictionary
95 error(const dictionary& errDict);
98 error(const error& err);
103 virtual ~error() throw();
108 string message() const;
110 const string& functionName() const
112 return functionName_;
115 const string& sourceFileName() const
117 return sourceFileName_;
120 label sourceFileLineNumber() const
122 return sourceFileLineNumber_;
125 void throwExceptions()
127 throwExceptions_ = true;
130 void dontThrowExceptions()
132 throwExceptions_ = false;
135 //- Convert to Ostream
136 // Prints basic message and then returns Ostream for further info.
139 const char* functionName,
140 const char* sourceFileName,
141 const int sourceFileLineNumber = 0
146 const string& functionName,
147 const char* sourceFileName,
148 const int sourceFileLineNumber = 0
151 //- Convert to Ostream
152 // Prints basic message and then returns Ostream for further info.
153 operator OSstream&();
155 //- Explicitly convert to Ostream 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& os);
168 //- Exit : can be called for any error to exit program. Prints stack
170 void exit(const int errNo = 1);
172 //- Abort : used to stop code for fatal errors. Prints stack before
179 friend Ostream& operator<<(Ostream&, const error&);
183 // Forward declaration of friend functions and operators
187 Ostream& operator<<(Ostream&, const IOerror&);
190 /*---------------------------------------------------------------------------*\
191 Class IOerror Declaration
192 \*---------------------------------------------------------------------------*/
194 //- Report an I/O error
202 label ioStartLineNumber_;
203 label ioEndLineNumber_;
210 //- Construct from title string
211 IOerror(const string& title);
213 //- Construct from dictionary
214 IOerror(const dictionary& errDict);
219 virtual ~IOerror() throw();
224 const string& ioFileName() const
229 label ioStartLineNumber() const
231 return ioStartLineNumber_;
234 label ioEndLineNumber() const
236 return ioEndLineNumber_;
239 //- Convert to Ostream
240 // Prints basic message and then returns Ostream for further info.
243 const char* functionName,
244 const char* sourceFileName,
245 const int sourceFileLineNumber,
246 const string& ioFileName,
247 const label ioStartLineNumber = -1,
248 const label ioEndLineNumber = -1
251 //- Convert to Ostream
252 // Prints basic message and then returns Ostream for further info.
255 const char* functionName,
256 const char* sourceFileName,
257 const int sourceFileLineNumber,
261 //- Convert to Ostream
262 // Prints basic message and then returns Ostream for further info.
265 const char* functionName,
266 const char* sourceFileName,
267 const int sourceFileLineNumber,
271 //- Create and return a dictionary
272 operator dictionary() const;
275 //- Exit : can be called for any error to exit program
276 void exit(const int errNo = 1);
278 //- Abort : used to stop code for fatal errors
284 friend Ostream& operator<<(Ostream&, const IOerror&);
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 // Global error declarations: defined in error.C
291 extern error FatalError;
292 extern IOerror FatalIOError;
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 // Convienient macros to add the file name and line number to the function name
297 #define FatalErrorIn(fn) FatalError(fn, __FILE__, __LINE__)
298 #define FatalIOErrorIn(fn, ios) FatalIOError(fn, __FILE__, __LINE__, ios)
300 // Call for functions which are not currently implemented.
301 // The functionName is printed and then abort is called.
302 #define notImplemented(fn) \
303 FatalErrorIn(fn) << "Not implemented" << Foam::abort(FatalError);
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
307 } // End namespace Foam
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
311 #include "errorManip.H"
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 // ************************************************************************* //