OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

error.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::error
00027 
00028 Description
00029     Class to handle errors and exceptions in a simple, consistent stream-based
00030     manner.
00031 
00032     The error class is globaly instantiated with a title string. Errors,
00033     messages and other data are piped to the messageStream class in the
00034     standard manner.  Manipulators are supplied for exit and abort which may
00035     terminate the program or throw an exception depending of if the exception
00036     handling has beed switched on (off by default).
00037 
00038 Usage
00039     @code
00040         error << "message1" << "message2" << FoamDataType << exit(errNo);
00041         error << "message1" << "message2" << FoamDataType << abort();
00042     @endcode
00043 
00044 SourceFiles
00045     error.C
00046 
00047 \*---------------------------------------------------------------------------*/
00048 
00049 #ifndef error_H
00050 #define error_H
00051 
00052 #include "messageStream.H"
00053 
00054 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00055 
00056 namespace Foam
00057 {
00058 
00059 // Forward declaration of friend functions and operators
00060 class error;
00061 Ostream& operator<<(Ostream&, const error&);
00062 
00063 
00064 /*---------------------------------------------------------------------------*\
00065                            Class error Declaration
00066 \*---------------------------------------------------------------------------*/
00067 
00068 class error
00069 :
00070     public std::exception,
00071     public messageStream
00072 {
00073 
00074 protected:
00075 
00076     // Protected data
00077 
00078         string functionName_;
00079         string sourceFileName_;
00080         label sourceFileLineNumber_;
00081 
00082         bool abort_;
00083 
00084         bool throwExceptions_;
00085         OStringStream* messageStreamPtr_;
00086 
00087 public:
00088 
00089     // Constructors
00090 
00091         //- Construct from title string
00092         error(const string& title);
00093 
00094         //- Construct from dictionary
00095         error(const dictionary& errDict);
00096 
00097         //- Construct as copy
00098         error(const error& err);
00099 
00100 
00101     // Destructor
00102 
00103         virtual ~error() throw();
00104 
00105 
00106     // Member functions
00107 
00108         string message() const;
00109 
00110         const string& functionName() const
00111         {
00112             return functionName_;
00113         }
00114 
00115         const string& sourceFileName() const
00116         {
00117             return sourceFileName_;
00118         }
00119 
00120         label sourceFileLineNumber() const
00121         {
00122             return sourceFileLineNumber_;
00123         }
00124 
00125         void throwExceptions()
00126         {
00127             throwExceptions_ = true;
00128         }
00129 
00130         void dontThrowExceptions()
00131         {
00132             throwExceptions_ = false;
00133         }
00134 
00135         //- Convert to Ostream
00136         //  Prints basic message and then returns Ostream for further info.
00137         OSstream& operator()
00138         (
00139             const char* functionName,
00140             const char* sourceFileName,
00141             const int sourceFileLineNumber = 0
00142         );
00143 
00144         OSstream& operator()
00145         (
00146             const string& functionName,
00147             const char* sourceFileName,
00148             const int sourceFileLineNumber = 0
00149         );
00150 
00151         //- Convert to Ostream
00152         //  Prints basic message and then returns Ostream for further info.
00153         operator OSstream&();
00154 
00155         //- Explicitly convert to Ostream for << operations
00156         OSstream& operator()()
00157         {
00158             return operator OSstream&();
00159         }
00160 
00161         //- Create and return a dictionary
00162         operator dictionary() const;
00163 
00164 
00165         //- Helper function to print a stack
00166         static void printStack(Ostream& os);
00167 
00168         //- Exit : can be called for any error to exit program. Prints stack
00169         //  before exiting.
00170         void exit(const int errNo = 1);
00171 
00172         //- Abort : used to stop code for fatal errors. Prints stack before
00173         //  exiting.
00174         void abort();
00175 
00176 
00177     // Ostream operator
00178 
00179         friend Ostream& operator<<(Ostream&, const error&);
00180 };
00181 
00182 
00183 // Forward declaration of friend functions and operators
00184 
00185 class IOerror;
00186 
00187 Ostream& operator<<(Ostream&, const IOerror&);
00188 
00189 
00190 /*---------------------------------------------------------------------------*\
00191                            Class IOerror Declaration
00192 \*---------------------------------------------------------------------------*/
00193 
00194 //- Report an I/O error
00195 class IOerror
00196 :
00197     public error
00198 {
00199     // Private data
00200 
00201         string ioFileName_;
00202         label ioStartLineNumber_;
00203         label ioEndLineNumber_;
00204 
00205 
00206 public:
00207 
00208     // Constructors
00209 
00210         //- Construct from title string
00211         IOerror(const string& title);
00212 
00213         //- Construct from dictionary
00214         IOerror(const dictionary& errDict);
00215 
00216 
00217     // Destructor
00218 
00219         virtual ~IOerror() throw();
00220 
00221 
00222     // Member functions
00223 
00224         const string& ioFileName() const
00225         {
00226             return ioFileName_;
00227         }
00228 
00229         label ioStartLineNumber() const
00230         {
00231             return ioStartLineNumber_;
00232         }
00233 
00234         label ioEndLineNumber() const
00235         {
00236             return ioEndLineNumber_;
00237         }
00238 
00239         //- Convert to Ostream
00240         //  Prints basic message and then returns Ostream for further info.
00241         OSstream& operator()
00242         (
00243             const char* functionName,
00244             const char* sourceFileName,
00245             const int sourceFileLineNumber,
00246             const string& ioFileName,
00247             const label ioStartLineNumber = -1,
00248             const label ioEndLineNumber = -1
00249         );
00250 
00251         //- Convert to Ostream
00252         //  Prints basic message and then returns Ostream for further info.
00253         OSstream& operator()
00254         (
00255             const char* functionName,
00256             const char* sourceFileName,
00257             const int sourceFileLineNumber,
00258             const IOstream&
00259         );
00260 
00261         //- Convert to Ostream
00262         //  Prints basic message and then returns Ostream for further info.
00263         OSstream& operator()
00264         (
00265             const char* functionName,
00266             const char* sourceFileName,
00267             const int sourceFileLineNumber,
00268             const dictionary&
00269         );
00270 
00271         //- Create and return a dictionary
00272         operator dictionary() const;
00273 
00274 
00275         //- Exit : can be called for any error to exit program
00276         void exit(const int errNo = 1);
00277 
00278         //- Abort : used to stop code for fatal errors
00279         void abort();
00280 
00281 
00282     // Ostream operator
00283 
00284         friend Ostream& operator<<(Ostream&, const IOerror&);
00285 };
00286 
00287 
00288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00289 // Global error declarations: defined in error.C
00290 
00291 extern error FatalError;
00292 extern IOerror FatalIOError;
00293 
00294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00295 // Convienient macros to add the file name and line number to the function name
00296 
00297 #define FatalErrorIn(fn) FatalError(fn, __FILE__, __LINE__)
00298 #define FatalIOErrorIn(fn, ios) FatalIOError(fn, __FILE__, __LINE__, ios)
00299 
00300 // Call for functions which are not currently implemented.
00301 // The functionName is printed and then abort is called.
00302 #define notImplemented(fn) \
00303     FatalErrorIn(fn) << "Not implemented" << Foam::abort(FatalError);
00304 
00305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00306 
00307 } // End namespace Foam
00308 
00309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00310 
00311 #include "errorManip.H"
00312 
00313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00314 
00315 #endif
00316 
00317 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd