Errors - how do you do it?

Started by
0 comments, last by Jx 22 years, 8 months ago
I am currently designing an engine and I am considering different ways of handling errors. I have come up with a solution but i''m not to sure whether it is the right one: I will define a CError class which looks something like this:
  
CError
{
	private:
	
		string ErrorMessage;

	public:
		
		string GetErrorMessage();
		
		CError( char *ErrorMessage );
		~CError( );
};
  
Normal accessors and mutators do not return CErrors Costructors and destructors throw CErrors Any other function returns a CError This means that your code can decide what to do with any errors it recieves whilst you can still check for errors in constructors and destructors using a try/catch block around you main program. Does this sound logical or an I just talking nonsense Any help would be good Thanks Justin
Advertisement
Instead of storing a string in the class, store an integer. You can use a lookup table when the user actually wants to check the error message.

There''s a few reasons for this, the biggest one is that constructing the CError class shouldn''t take a long time, and allocation memory for and copying a string is probably too time consuming. Especially if (almost) every function does it.

Another good reason is that testing for success is fast (just test for equality to zero, rather than a string compare).

And finally, you can easily localise your error messages by just replacing the lookup table with whatever language the user wants (and if they report the error, just make sure they give the ID as well, so you can look it up in your own preffered language).

It''s good to see you thinking about error handling at this (aparently) early stage. Most people don''t bother and end with about 4 different ways of handling errors by the time they finish.

codeka.com - Just click it.

This topic is closed to new replies.

Advertisement