checking for specific errors in catch

S

SilentCry

this is driving me nuts.. i'm trying to do something that i thought would be
relatively simple but it's not turning out that way. all i want to do is
check for a specific HResult of 0x80004005 in my catch stmt..

catch (OleDbException ex)
{
if (ex.ErrorCode != 0x80004005) // duplicate record, OK - anything
else, not
throw;
}

first, ErrorCode is defined as a property returning an int but described as
returning the HResult of the operation. so which is it, int or HResult??
i tried casting the constant as an int but that didn't work. the error said
something about using the unchecked macro.
i tried casting the constant as an HRESULT but the compiler didn't recognize
that type.
i tried doing a Convert.ToInt16 and Convert.ToInt32 on the constant but got
overflow errors at run time.

what the heck is going on here? i must be missing something but i don't know
what. seems to me it should be a lot simpler than this.
 
G

Guest

Hi

I'm not sure you really need to be doing any casting at all
ErrorCode does return the HResult (as an int already). All you really need to do is convert 0x80004005 to an interger, which I believe is 2147500037. Just use that for the comparison and it should work.

HT

Eddie de Bear
 
S

SilentCry

thanx for the reply but after playing with it, apparently i had to use all
32 bits (int = 32-bit integer) in the constant i was comparing against.
looking at the errorcode in debug, the value returned was a much larger and
negative 10-digit number that when converted to hex comes out to
0xFFFFFFFF80004005. when i used the negative number in the if, it worked.
seems to me you should have the ability to use just the 80004005 portion of
the errorcode without having to worry about typing.

Eddie said:
Hi,

I'm not sure you really need to be doing any casting at all.
ErrorCode does return the HResult (as an int already). All you really need
to do is convert 0x80004005 to an interger, which I believe is 2147500037.
Just use that for the comparison and it should work.
 
C

Chris R. Timmons

thanx for the reply but after playing with it, apparently i had
to use all 32 bits (int = 32-bit integer) in the constant i was
comparing against. looking at the errorcode in debug, the value
returned was a much larger and negative 10-digit number that
when converted to hex comes out to 0xFFFFFFFF80004005. when i
used the negative number in the if, it worked. seems to me you
should have the ability to use just the 80004005 portion of the
errorcode without having to worry about typing.


to do is convert 0x80004005 to an interger, which I believe is
2147500037. Just use that for the comparison and it should work.

Eddie,

I ran into the same problem with the COMException class. HRESULTs
are unsigned ints, but the ErrorCode property for both COMException
and OleDbException are ints. Here's how I handled it (C#):


private const uint DB_E_CANCELED = 0x80040E4E;

...

try
{
...
}
catch (COMException ce)
{
// User pressed the Cancel button.

// (Oversight by Microsoft(?)). The COMException.ErrorCode property
// is an Int32, but the HRESULT values are UInt32 values.
// In addition to the (int) cast, the "unchecked" statement is
// needed to keep the compiler from squawking.

if (ce.ErrorCode == unchecked((int) DB_E_CANCELED))
result = string.Empty;
else
throw ce;
}


Hope this helps.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top