views:

21

answers:

2

I have the following function that was written by someone else, however I am rewriting this application and I was just wondering if there isn't any better way to do exception handling, besides just returning what was originally passed to the function?

CComVariant GetFldVar(ADO_RsPtr rs, long nIndex, CComVariant def)
{
    try
    {
        return rs->GetFields()->GetItem(nIndex)->GetValue();
    }
    catch (...)
    {
        return def;  // catch exception and just return old variable?? Is that the right way to go about things??
    }
}
A: 

Def appears to be the default, i.e. the function tries to get column n out of the current row (rs) and if it fails, it returns the default. I don't think this is an error rather a particular use case. It's a fairly stanard pattern to allow the caller to avoid checking for missing values esp. from the db, and to specify a default one as a fallback.

Having said that, it could be an error as it swallows serisous errors, however maybe the code is written so that the caller can carry on regardless of the how the ADO call results.

Preet Sangha
A: 

What was passed to the function was what the caller thought it was a proper default (or even error-indicating) value. The caller now has the responsibility to check the result and determin what to do. The thing is that in this way he/she has no way of knowing what kind of error happened. He can check the Errors collection of the Connection, but they are only ADO errors (though I don't think this code has room for other kind of errors). Other than that, tis way seems to me to work good.

frag