views:

42

answers:

3

i'm talking to a COM object (Microsoft ADO Recordset object). In a certain case the recordset will return a failed (i.e. negative) HRESULT, with the message:

Item cannot be found in the collection corresponding to the requested name or ordinal

i know what this error message means, know why it happened, and i how to fix it. But i know these things because i read the message, which fortunately was in a language i understand.

Now i would like to handle this exception specially. The COM object threw an HRESULT of

0x800A0CC1

In an ideal world Microsoft would have documented what errors can be returned when i try to access:

records.Fields.Items( index )

with an invalid index. But they do not; they most they say is that an error can occur, i.e.:

If Item cannot find an object in the collection corresponding to the Index argument, an error occurs.

Given that the returned error code is not documented, is it correct to handle a specific return code of `0x800A0CC1' when i'm trying to trap the exception:

Item cannot be found in the collection corresponding to the requested name or ordinal

?

Since Microsoft didn't document the error code, they technically change it in the future.

+3  A: 

You'll have to decide whether or not it is worth the risk, but I believe that it is unlikely that Microsoft will change this error code. Checking for this particular error code is a pretty robust way to go.

Robert Harvey
Agreed - when you Google the error code, you can find posts referring to it that date back to 2002 on the first results page, so it hasn't changed for at least that long.
Paddyslacker
After reading Raymond Chen's blog for so long, i'm acutely aware of how wrong it is to rely on undocumented behavior; it limits Microsoft's ability to improve and innovate.
Ian Boyd
+2  A: 

They did document this error code, but it's hard to find: ErrorValueEnum:

adErrItemNotFound    3265 -2146825023 0x800A0CC1    Item cannot be found in the collection that corresponds to the requested name or ordinal.

..so, as its' a documented error code, it is safe to test for it explicitly.

JBRWilkinson
Two identical answers mention that the documentation for errors thrown by the property are not documented with the property. But yours gets the accepted because you quoted the docs, and linked to it. Plus, with 1k rep you keep the accepted answers more than Hans (Sorry Hans, but it's true)
Ian Boyd
+1  A: 

Yes, it is fine. It is in fact a documented error code, although finding them is never easy. It is defined in the msdao15.idl Windows SDK file, adErrItemNotFound (error 3265)

Hans Passant