Archived

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

Anarchi

Return code techniques - what do u prefer?

Recommended Posts

Anarchi    122
Here are the types of function return systems ive come across: 1) GetLastError() "bool LoadBitmap(char szFile)" If( ! LoadBitmap("MyBitmap.bmp") ) { if(GetLastError() == ERR_NOTFOUND) msg("bitmap not found"); if(GetLastError() == ERR_FAILED) msg("bitmap invalid"); } 2) HRESULT "HRESULT LoadBitmap(char szFile)" HRESULT hr; hr = LoadBitmap(char szFile); if(FAILED(hr)) { if(hr == DDERR_FILENOTFOUND) msg("bitmap not found"); if(hr == DDERR_BITMAPINVALID) msg("bitmap invalid"); } With the HRESULT codes, is there a range of user-codes that I can use for my own functions so I can mix DirectX codes and my own defined codes?

Share this post


Link to post
Share on other sites
Z01    134
You can also throw an exception.

I''m sure there is a region of HRESULT values that is unused, but using them for your own error codes is bad because you never know when HRESULTs might be changed by microsoft. HERSULT is just a typedef:
  
typedef LONG HRESULT;

And the reason that MS did this is so they could hide the implementation of HRESULTs so they could change them without affecting all the code already in use.

Share this post


Link to post
Share on other sites
Anarchi    122
Ok, but say if I was to create a wrapper or a library, would it be wise to use my own error codes instead of using HRESULT?

For instance:

//header file
#define ERR_CANNOTFINDFILE 0x1000
#define ERR_INVALIDBITMAP 0x1001



// usage
DWORD dw;
dw = LoadBitmap(char szFile)
if(dw != ERR_OK)
{
if(dw == ERR_CANNOTFINDFILE ) msg("Cannot find file");
if(dw == ERR_INVALIDBITMAP ) msg("Invalid Bitmap");
}




Downloads, D3DXSprite tutorial, New platform game: .-= The ZeroOne Realm =-.

Share this post


Link to post
Share on other sites
Spiral    122
If i were you i''d make my own... using exceptions for fatal errors.

And for the love of god, dont implement a GetLastError() style approach. Horrid.

Share this post


Link to post
Share on other sites
Siebharinn    122
I do a kind of hybrid approach. I have a list of error constants, but usually use them through exceptions.

  
const int E_FILENOTFOUND = 0x0001;

if(!LoadBitmap("goober.bmp"))
throw ResourceException(E_FILENOTFOUND, "goober.bmp");

// although I'd just make my own LoadBitmap() that throws

// a resource exception, so I could do this:


try {
LoadBitmap("one.bmp");
LoadBitmap("two.bmp");
LoadBitmap("three.bmp");
LoadSound("one.wav");
}
catch(ResourceException& re) {
// handle the problem

}



Personal preference, I guess.

Take care,
Bill

Edited by - Siebharinn on February 6, 2002 10:12:40 AM

Share this post


Link to post
Share on other sites
zipless    122
I use try-throw-catch groups in nearly all of my code that is used in libraries, for return values i use a Defined error value usually something like LIB_ERROR -123456.

Then of course i''ll add a nice macro to the library to detect the error though i think using FAILED works fine because DX functions return negative values on a fail.

Inside non-library code i tend not to use ttc''s mainly because they take longer to implement thatn returning an int.

zipless

/* Ignorance is bliss, then you go and spoil it by learning stuff */

Share this post


Link to post
Share on other sites
KingSalami    122
Just return a freaking int and define you errors in an h file or use a try catch with a catch for COM errors and a seperate catch for all other errors.


catch(_com_error &e)
{
TRACE( "Error:%08lx.\n", e.Error());
TRACE( "ErrorMessage:%s.\n", e.ErrorMessage());
TRACE( "Source:%s.\n", (LPCTSTR) _bstr_t(e.Source()));
TRACE( "Description:%s.\n", (LPCTSTR) _bstr_t(e.Description()));
}

catch(...)
{
\\whatever...
}

Share this post


Link to post
Share on other sites