FreeFOAM The Cross-Platform CFD Toolkit
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 | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 Class
25  Foam::error
26 
27 Description
28  Class to handle errors and exceptions in a simple, consistent stream-based
29  manner.
30 
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).
36 
37 Usage
38  @code
39  error << "message1" << "message2" << FoamDataType << exit(errNo);
40  error << "message1" << "message2" << FoamDataType << abort();
41  @endcode
42 
43 SourceFiles
44  error.C
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef error_H
49 #define error_H
50 
51 #include "messageStream.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward declaration of friend functions and operators
59 class error;
60 Ostream& operator<<(Ostream&, const error&);
61 
62 
63 /*---------------------------------------------------------------------------*\
64  Class error Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 class error
68 :
69  public std::exception,
70  public messageStream
71 {
72 
73 protected:
74 
75  // Protected data
76 
77  string functionName_;
80 
81  bool abort_;
82 
85 
86 public:
87 
88  // Constructors
89 
90  //- Construct from title string
91  error(const string& title);
92 
93  //- Construct from dictionary
94  error(const dictionary& errDict);
95 
96  //- Construct as copy
97  error(const error& err);
98 
99 
100  // Destructor
101 
102  virtual ~error() throw();
103 
104 
105  // Member functions
106 
107  string message() const;
108 
109  const string& functionName() const
110  {
111  return functionName_;
112  }
113 
114  const string& sourceFileName() const
115  {
116  return sourceFileName_;
117  }
118 
119  label sourceFileLineNumber() const
120  {
121  return sourceFileLineNumber_;
122  }
123 
125  {
126  throwExceptions_ = true;
127  }
128 
130  {
131  throwExceptions_ = false;
132  }
133 
134  //- Convert to Ostream
135  // Prints basic message and then returns Ostream for further info.
136  OSstream& operator()
137  (
138  const char* functionName,
139  const char* sourceFileName,
140  const int sourceFileLineNumber = 0
141  );
142 
143  OSstream& operator()
144  (
145  const string& functionName,
146  const char* sourceFileName,
147  const int sourceFileLineNumber = 0
148  );
149 
150  //- Convert to Ostream
151  // Prints basic message and then returns Ostream for further info.
152  operator OSstream&();
153 
154  //- Explicitly convert to Ostream for << operations
156  {
157  return operator OSstream&();
158  }
159 
160  //- Create and return a dictionary
161  operator dictionary() const;
162 
163 
164  //- Helper function to print a stack
165  static void printStack(Ostream& os);
166 
167  //- Exit : can be called for any error to exit program. Prints stack
168  // before exiting.
169  void exit(const int errNo = 1);
170 
171  //- Abort : used to stop code for fatal errors. Prints stack before
172  // exiting.
173  void abort();
174 
175 
176  // Ostream operator
177 
178  friend Ostream& operator<<(Ostream&, const error&);
179 };
180 
181 
182 // Forward declaration of friend functions and operators
183 
184 class IOerror;
185 
186 Ostream& operator<<(Ostream&, const IOerror&);
187 
188 
189 /*---------------------------------------------------------------------------*\
190  Class IOerror Declaration
191 \*---------------------------------------------------------------------------*/
192 
193 //- Report an I/O error
194 class IOerror
195 :
196  public error
197 {
198  // Private data
199 
200  string ioFileName_;
201  label ioStartLineNumber_;
202  label ioEndLineNumber_;
203 
204 
205 public:
206 
207  // Constructors
208 
209  //- Construct from title string
210  IOerror(const string& title);
211 
212  //- Construct from dictionary
213  IOerror(const dictionary& errDict);
214 
215 
216  // Destructor
217 
218  virtual ~IOerror() throw();
219 
220 
221  // Member functions
222 
223  const string& ioFileName() const
224  {
225  return ioFileName_;
226  }
227 
228  label ioStartLineNumber() const
229  {
230  return ioStartLineNumber_;
231  }
232 
233  label ioEndLineNumber() const
234  {
235  return ioEndLineNumber_;
236  }
237 
238  //- Convert to Ostream
239  // Prints basic message and then returns Ostream for further info.
240  OSstream& operator()
241  (
242  const char* functionName,
243  const char* sourceFileName,
244  const int sourceFileLineNumber,
245  const string& ioFileName,
246  const label ioStartLineNumber = -1,
247  const label ioEndLineNumber = -1
248  );
249 
250  //- Convert to Ostream
251  // Prints basic message and then returns Ostream for further info.
252  OSstream& operator()
253  (
254  const char* functionName,
255  const char* sourceFileName,
256  const int sourceFileLineNumber,
257  const IOstream&
258  );
259 
260  //- Convert to Ostream
261  // Prints basic message and then returns Ostream for further info.
262  OSstream& operator()
263  (
264  const char* functionName,
265  const char* sourceFileName,
266  const int sourceFileLineNumber,
267  const dictionary&
268  );
269 
270  //- Create and return a dictionary
271  operator dictionary() const;
272 
273 
274  //- Exit : can be called for any error to exit program
275  void exit(const int errNo = 1);
276 
277  //- Abort : used to stop code for fatal errors
278  void abort();
279 
280 
281  // Ostream operator
282 
283  friend Ostream& operator<<(Ostream&, const IOerror&);
284 };
285 
286 
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 // Global error declarations: defined in error.C
289 
290 extern error FatalError;
291 extern IOerror FatalIOError;
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 // Convienient macros to add the file name and line number to the function name
295 
296 #define FatalErrorIn(fn) FatalError(fn, __FILE__, __LINE__)
297 #define FatalIOErrorIn(fn, ios) FatalIOError(fn, __FILE__, __LINE__, ios)
298 
299 // Call for functions which are not currently implemented.
300 // The functionName is printed and then abort is called.
301 #define notImplemented(fn) \
302  FatalErrorIn(fn) << "Not implemented" << Foam::abort(FatalError);
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 } // End namespace Foam
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #include "errorManip.H"
311 
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
313 
314 #endif
315 
316 // ************************ vim: set sw=4 sts=4 et: ************************ //