I'm trying to catch and handle a specific HttpException, namely "The remote host closed the connection. The error code is 0x800704CD."

My intention is to add a Catch for the HttpException to the relevant Try block and test for the error code that is generated. Sample code:

Try
    // Do some stuff
Catch exHttp As HttpException
    If exHttp.ErrorCode.ToString() = "0x800704CD" Then DoSomething()
Catch ex As Exception
    // Generic error handling
End Try

But I can't work out how to extract the error code displayed in the exception (i.e. "0x800704CD") from the HttpException object. Converting the integer value of the ErrorCode property to hex returns "800704CD" so clearly I'm not understanding how this code is generated.

Thanks.

有帮助吗?

解决方案

Try the Below Code:

 Try
    // Do some stuff
        Catch exHttp As HttpException
            If exHttp.ErrorCode = &H800704CD Then DoSomething()
        Catch ex As Exception
    // Generic error handling
        End Try

其他提示

ErrorCode property is an integer so just test for integer value, no reason to convert to hexadecimal or string.

0x in most programming languages means the following characters denote a hexadecimal number, i.e. 0x800704CD means 800704CD will be interpreted as a hexadecimal number. For VB use &H.

More on VB.Net literals:

http://msdn.microsoft.com/en-us/library/s9cz43ek.aspx

http://msdn.microsoft.com/en-us/library/dzy06xhf.aspx

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top