Skip to main content
gamedev.net
🔒 Locked

Exception Error object

Started by Toolmaker Sep 8, 2003 at 4:23 AM 6 replies 750+ views
Original Post
Toolmaker
Toolmaker
I am currently slowly starting to develop a bunch custom made libraries and classes. The first thing I was thinking about was to create an Error object, which I could use when throwing an exception. So, instead of doing throw 0; I would do throw CError(100, "Error code 100"); But, is this actually possible when throwing exceptions, and to what things do I need to pay attention to? I was thinking of storing the following data:
long lErrorNumber;
char szErrorText[512]; // 512 chars is enough for error msg imho
 
Toolmaker
-Earth is 98% full. Please delete anybody you can.
 
programering
programering
Do as you said, I don''t know?

-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Packa bajs med winzip!

Just dreaming wont make you (more) skilled in game programming/development.
stubble
stubble
It''s possible and a good thing. You can have specialised subclasses of CError for identifying certain types of error etc.

I few things I would remember though:

Exceptions are relativly expensive - so I''d only use them for fatal errors in any time critical code - ie a main game loop.

Also catch references to the object rather than intances (for obvious reasons. eg. catch(CError & e)
NatasDM
NatasDM
quote:
Original post by Toolmaker
I would do throw CError(100, "Error code 100");

long lErrorNumber;
char szErrorText[512]; // 512 chars is enough for error msg imho


Toolmaker



-Earth is 98% full. Please delete anybody you can.


Try this:

struct ErrorObject
{
long lErrorNumber;
char szErrorText[512]; //I would use std::string

ErrorObject( long ax, const char* bx): lErrorNumber(ax)
{strcpy(szErrorText, bx);}
}


That way, when you throw an error, you can throw it like this:
throw ErrorObject(100,"Error Code 100"); 


Then, if you think of anything else to add to the struct (other pertinent information), you don''t have to rewrite all of your code, you simply have to add a second constructor (ctor) and the new member:


{
long lErrorNumber;
char szFileName[512];
char szErrorText[512]; //I would use std::string

ErrorObject( long ax, const char* bx): lErrorNumber(ax)
{strcpy(szErrorText, bx);}
ErrorObject( long ax, const char* bx, const char* cx): lErrorNumber(ax)
{strcpy(szErrorText, bx);strcpy(szFileName,cx);}
}

Now the old

throw ErrorObject(100,"Error 100");

will still work, as well as:

throw ErrorObject(100,"Error 100", __FILE__);
Lord Bart
Lord Bart
Hello Toolmaker,

Do as Lektrix says and make sure your exception class inherit off std::exception, and also make sure to implement all the methods std::exception has to. After this like one poster says about string and memory could be a problem. So you might not want to use a class that has std::string in it if the exception would come from a memory error. Also the __LINE__ and __FILE__ macros are real usefule to pass into an exception consturctor.

Lord Bart
Toolmaker
Toolmaker
Actually, that is the reason I use a char array in my shown example. But, as AP showed, I switch it to a const char pointer.

Anyway, I now have a good idea on how to develop my class and I will develop it now. At least the first project to use it for will be my custom string class

Toolmaker



-Earth is 98% full. Please delete anybody you can.
 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.