Programming 2D Games

The official forum for "Programming 2D Games" the book by: Charles Kelly

It is currently Fri Nov 16, 2018 2:53 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: GameError class revisted
PostPosted: Mon Aug 03, 2015 11:41 pm 
Offline

Joined: Sun Aug 02, 2015 2:52 pm
Posts: 15
The GameError class defines an exception class to throw exceptions in the following manner

Code:
throw(GameError(gameErrorNS::FATAL_ERROR, "Error creating Direct3D device"));

What caught my eye was "FATAL_ERROR." "What's that about?", I wondered. FATAL_ERROR is defined in gameError.h. This leads to an inspection of the GameError exception class.
Code:
//...   
// Error codes
// Negative numbers are fatal errors that may require the game to be shutdown.
// Positive numbers are warnings that do not require the game to be shutdown.
const int FATAL_ERROR = -1;
const int WARNING = 1;
//...
   
// Game Error class. Thrown when an error is detected by the game engine.
// Inherits from std::exception
class GameError : public std::exception
{
private:
    int errorCode;
    std::string message;
public:
    GameError() throw();
    GameError(const GameError& e) throw();
    GameError(int code, const std::string &s) throw();
    virtual ~GameError() throw();

    GameError& operator= (const GameError& rhs) throw();

    virtual const char* what() const throw();
    const char* getMessage() const throw();
    int getErrorCode() const throw();
};

As mentioned in the post "A question regarding exception specifications", the throw specification is no longer considered a good idea and I think it has been officially depreciated. But it does lead to something.

If we actually used the exception error code we would have something like the following

Code:
try
{
    //...   
} catch(const GameError& msg) {
    switch (msg.getErrorCode())
    {
    case FATAL_ERROR:
        //...
    case WARNING:
        //...
    case NEVER_ON_TUESDAY:
        //...
    }
} catch(...) {
    //...   
}

I don't think this is how exceptions should be used. Interestingly, I could find no instance of an exception thrown with a WARNING value.

A real question is, "How do you distinguish different types of exceptions?" The answer is you define distinct types for them. You can't simply have all your exceptions derive from std::exception

Code:
typedef std::exception GameError;
typedef std::exception GameError1;
typedef std::exception GameError2;

This won't work because "Exceptions for a base class catch exceptions for a derived class."

Defining distinct exceptions types is really the way to go. You shouldn't need very many exception types. Since there is really only one exception type, FATAL_ERROR, you can use the typedef for it. You can still have the custom strings beccause std::exception constructor takes a string argument.

- tim


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group