• Advertisement

Archived

This topic is now archived and is closed to further replies.

Exceptions and Logging

This topic is 5064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is sort of a long post, but It's a design question, and I couldn't figure out any way to shorten it, let me say in advance I thank anyone who takes the time to read & comprehend my question. In my current project when an error occurs a function that looks like logError(ErrorSeverity, const char*, ...) is called. This I have found is very handy, and is used in many classes. some example psuedocode may help me explain:
void Image::Draw()
{
   if(!loaded)
       logError(ERR_WARNING,"Call to Image::Draw with no Image loaded.");
   else {  ... }    
}
  
then I have more severe errors like
bool VideoCore::CreateDisplay()
{
    ...
    if(device == INVALID)
    {
        logError(ERR_CRITICAL,"Failed to create device in CreateDisplay");
        return false;
    }
    ...
}
  
I was thinking it'd be good to start using exceptions, I haven't used them since I was first learning C++, but while freshening up on them I realized they would help me a good deal. I know I could continue calling logError where I currently am, and on the more severe errors throw an appropriate error, but then I was thinking instead I could have calls to logError which had a severity higher than a given level could throw. So logError would have new code that looked like
if(severity >= ERR_CRITICAL)
    throw Exception(severity,desc);
  
And no major changes would be required in the code that calls logError. Before I went through making the changes necessary to implement this design, I wanted to air it out on these forums, to see if anyone more experienced with exceptions would say "what are you doing, idiot" or even suggest something better. edit: code tags & typo [edited by - cozman on April 11, 2004 2:40:05 AM]

Share this post


Link to post
Share on other sites
Advertisement
I generally consider it to be a Good Thing to throw exceptions if something happens which should not happen (at runtime).

You game can then fail reasonably gracefully (for instance telling the user an error has occurred, cleaning up the gfx hardware)

Depending on how your program works, drawing an image which hasn''t been loaded could be such a thing.

Having a message is useful, as it can be placed on a log so that you can do some field debugging (those annoying errors which can''t easily be reproduced in development).

I was looking at trying to determine whether there was a reasonable way of dumping a stack trace in C++; there certainly isn''t a portable one. I usually end up either using a macro to dump __FILE__ and __LINE__ into the exception, or putting a custom string in.

If you''re using C library calls or Win32 library calls, it may be worth writing an exception class which in its constructor looks up the error number and message appropriately. Then throw that exception type if a C or win32 library function fails unexpectedly.

Mark

Share this post


Link to post
Share on other sites

  • Advertisement