How Can I Determine what type of IOException occurred?

R

Rymfax

I've created a Download Manager application and I'm having a problem
with some clients where for whatever reason, the remote connection is
forceably closed (whether on the client side or server side I'm not
sure).

What I want to do is catch the specific IOException that deals with
the that only. I know with the WebException there is a Status and
then a related WebExceptionStatus enum that lets me distinguish
between the different WebExceptions. Is there an equivallent for
IOException? If not, how do I determine how to distinguish, other
than parsing the Message property, which I really don't want to do
because it isn't very clean.

Here's an example of the error I'm getting:

Error: Unable to read data from the transport connection: An existing
connection was forcibly closed by the remote host.
Stack: at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset,
Int32 size)


Any help is appreciated.

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

Rymfax,

There isn't really much you can do without violating encapsulation rules
(in order to get the HResult through reflection, and even then, it's
unlikely it will give you a clear answer, since the hresult doesn't always
map one to one to the actuall error).

Depending on what you are doing, you might have to drop down to use an
API which will give you more detailed error information.
 
R

Rymfax

Rymfax,

There isn't really much you can do without violating encapsulation rules
(in order to get the HResult through reflection, and even then, it's
unlikely it will give you a clear answer, since the hresult doesn't always
map one to one to the actuall error).

Depending on what you are doing, you might have to drop down to use an
API which will give you more detailed error information.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I've created a Download Manager application and I'm having a problem
with some clients where for whatever reason, the remote connection is
forceably closed (whether on the client side or server side I'm not
sure).
What I want to do is catch the specific IOException that deals with
the that only. I know with the WebException there is a Status and
then a related WebExceptionStatus enum that lets me distinguish
between the different WebExceptions. Is there an equivallent for
IOException? If not, how do I determine how to distinguish, other
than parsing the Message property, which I really don't want to do
because it isn't very clean.
Here's an example of the error I'm getting:
Error: Unable to read data from the transport connection: An existing
connection was forcibly closed by the remote host.
Stack: at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset,
Int32 size)
Any help is appreciated.
Thanks!- Hide quoted text -

- Show quoted text -

Thanks for the input Nic. Just as an FYI, I did find a solution.
Apparently, when there is an IOException, there is usually an
InnerException associated with it. The InnerException in this case
Happens to be a SocketException, which has a SocketErrorCode
associated with it. Using that, I can determine what type of
SocketError it is and go from there.
 
N

Nicholas Paldino [.NET/C# MVP]

Rymfax,

Ahh, I forgot about that (duh on my part). Just note that the
InnerException will not always be available, depending on the operation that
threw the IOException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rymfax said:
Rymfax,

There isn't really much you can do without violating encapsulation
rules
(in order to get the HResult through reflection, and even then, it's
unlikely it will give you a clear answer, since the hresult doesn't
always
map one to one to the actuall error).

Depending on what you are doing, you might have to drop down to use
an
API which will give you more detailed error information.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I've created a Download Manager application and I'm having a problem
with some clients where for whatever reason, the remote connection is
forceably closed (whether on the client side or server side I'm not
sure).
What I want to do is catch the specific IOException that deals with
the that only. I know with the WebException there is a Status and
then a related WebExceptionStatus enum that lets me distinguish
between the different WebExceptions. Is there an equivallent for
IOException? If not, how do I determine how to distinguish, other
than parsing the Message property, which I really don't want to do
because it isn't very clean.
Here's an example of the error I'm getting:
Error: Unable to read data from the transport connection: An existing
connection was forcibly closed by the remote host.
Stack: at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset,
Int32 size)
Any help is appreciated.
Thanks!- Hide quoted text -

- Show quoted text -

Thanks for the input Nic. Just as an FYI, I did find a solution.
Apparently, when there is an IOException, there is usually an
InnerException associated with it. The InnerException in this case
Happens to be a SocketException, which has a SocketErrorCode
associated with it. Using that, I can determine what type of
SocketError it is and go from there.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Rymfax said:
I've created a Download Manager application and I'm having a problem
with some clients where for whatever reason, the remote connection is
forceably closed (whether on the client side or server side I'm not
sure).

What I want to do is catch the specific IOException that deals with
the that only. I know with the WebException there is a Status and
then a related WebExceptionStatus enum that lets me distinguish
between the different WebExceptions. Is there an equivallent for
IOException? If not, how do I determine how to distinguish, other
than parsing the Message property, which I really don't want to do
because it isn't very clean.

Here's an example of the error I'm getting:

Error: Unable to read data from the transport connection: An existing
connection was forcibly closed by the remote host.
Stack: at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset,
Int32 size)

Unless a sub class of IOException is actually thrown, then there
are not much you can do.

Arne
 

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