Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/util/Exception.h @ 2266

Last change on this file since 2266 was 2259, checked in by rgrieder, 16 years ago

The idea with exceptions is that you can throw them without having the user to actually see. Imagine a parser: Whenever it encounters something really bad, an exception is thrown. But the message you would like to display might be much nicer, for instance "Could not parse the selected file. Error in line ##".
Btw: There is a little explanation just above the code in Exception.h.

  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/objecthierarchy/src/util/Exception.hmergedeligible
    /code/branches/ceguilua/src/util/Exception.h1802-1808
    /code/branches/core3/src/core/Exception.h1572-1739
    /code/branches/gcc43/src/core/Exception.h1580
    /code/branches/gui/src/core/Exception.h1635-1723
    /code/branches/input/src/core/Exception.h1629-1636
    /code/branches/pickups/src/util/Exception.h1926-2086
    /code/branches/questsystem/src/util/Exception.h1894-2088
    /code/branches/weapon/src/util/Exception.h1925-2094
File size: 5.1 KB
RevLine 
[1638]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
31@brief
32    Declaration of the Exception class.
33*/
34
35#ifndef _Exception_H__
36#define _Exception_H__
37
[1764]38#include "UtilPrereqs.h"
[1638]39
40#include <string>
41#include <exception>
[1642]42#include <cassert>
[1764]43#include "Debug.h"
[1638]44
45namespace orxonox
46{
[1764]47    class _UtilExport Exception : public std::exception
[1638]48    {
49    public:
50
51        Exception(const std::string& description, int lineNumber,
[2103]52                  const char* filename, const char* functionName);
[1638]53        Exception(const std::string& description);
54
55        /// Needed for  compatibility with std::exception (from Ogre::Exception)
56        virtual ~Exception() throw() { }
57
[1664]58        virtual const std::string& getFullDescription() const;
[1638]59        virtual std::string        getTypeName()        const = 0;
60        virtual const std::string& getDescription()     const { return this->description_; }
61        virtual const int          getLineNumber()      const { return this->lineNumber_; }
62        virtual const std::string& getFunctionName()    const { return this->functionName_; }
63
64        /// Override std::exception::what (from Ogre::Exception)
65        const char* what() const throw() { return getFullDescription().c_str(); }
66
67    protected:
68        std::string description_;
69        int lineNumber_;
70        std::string functionName_;
[2103]71        std::string filename_;
[1670]72        // mutable because "what()" is a const method
[1664]73        mutable std::string fullDescription_;
[1638]74    };
75
76
[2171]77#define CREATE_ORXONOX_EXCEPTION(ExceptionName)                                     \
78    class ExceptionName##Exception : public Exception                               \
79    {                                                                               \
80    public:                                                                         \
81        ExceptionName##Exception(const std::string& description, int lineNumber,    \
82                  const char* filename, const char* functionName)                   \
83                  : Exception(description, lineNumber, filename, functionName)      \
84        {                                                                           \
85            /* Let the catcher decide whether to display the message below level 4  \
86               Note: Don't place this code in Exception c'tor because getTypeName() \
87               is still pure virtual at that time. */                               \
[2259]88            COUT(4) << this->getFullDescription() << std::endl;                     \
[2171]89        }                                                                           \
90                                                                                    \
91        ExceptionName##Exception(const std::string& description)                    \
92                  : Exception(description)                                          \
[2259]93        { COUT(4) << this->getFullDescription() << std::endl; }                     \
[2171]94                                                                                    \
95        ~ExceptionName##Exception() throw() { }                                     \
96                                                                                    \
97        std::string getTypeName() const { return #ExceptionName; }                  \
[1638]98    };
99
[2171]100    // Creates all possible exception types.
101    // If you want to add a new type, simply copy and adjust a new line here.
[1660]102    CREATE_ORXONOX_EXCEPTION(General);
103    CREATE_ORXONOX_EXCEPTION(FileNotFound);
[1663]104    CREATE_ORXONOX_EXCEPTION(Argument);
[1660]105    CREATE_ORXONOX_EXCEPTION(PluginsNotFound);
106    CREATE_ORXONOX_EXCEPTION(InitialisationFailed);
107    CREATE_ORXONOX_EXCEPTION(NotImplemented);
108    CREATE_ORXONOX_EXCEPTION(GameState);
[2171]109    CREATE_ORXONOX_EXCEPTION(NoGraphics);
110    CREATE_ORXONOX_EXCEPTION(AbortLoading);
111}
[1638]112
[2171]113#define ThrowException(Type, Description) \
114    throw Type##Exception(Description, __LINE__, __FILE__, __FUNCTIONNAME__)
[1638]115
[1642]116    // define an assert macro that can display a message
117#ifndef NDEBUG
[2171]118#define OrxAssert(Assertion, ErrorMessage) \
119    Assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << ErrorMessage << std::endl); \
120    assert(Assertion)
[1642]121#else
122#define OrxAssert(condition, errorMessage)  ((void)0)
123#endif
124
[1638]125#endif /* _Exception_H__ */
Note: See TracBrowser for help on using the repository browser.