Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM: How to handle a specific exception?

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.

like image 881
Ian Boyd Avatar asked Jun 01 '10 17:06

Ian Boyd


2 Answers

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.

like image 144
JBRWilkinson Avatar answered Sep 22 '22 13:09

JBRWilkinson


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.

like image 34
Robert Harvey Avatar answered Sep 25 '22 13:09

Robert Harvey