.NET Framework - [Exceptions Handling] How to understand the different type of errors

Asked By Starsky on 18-May-07 05:19 AM
Hi all,
I need to understand what kind of error is catched in a try/catch block
because in the catch section I need to do different things depending of the
error type (sockets & co)

I have these 2 situations (generated by a DataStream.EndRead operation),
the messages are in italian language

[1^ Situation]
{System.IO.IOException}
System.IO.IOException: {"Unable to read data from the transport
connection: Operazione di I/O terminata a causa dell'uscita dal thread
oppure della richiesta di un'applicazione."}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: Nothing
InnerException: {"Operazione di I/O terminata a causa dell'uscita dal
thread oppure della richiesta di un'applicazione"}
Message: "Unable to read data from the transport connection: Operazione
di I/O terminata a causa dell'uscita dal thread oppure della richiesta di
un'applicazione."
Source: "System"

[2^ Situation]
{System.IO.IOException}
System.IO.IOException: {"Unable to read data from the transport
connection: Connessione in corso interrotta forzatamente dall'host
remoto."}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: Nothing
InnerException: {"Connessione in corso interrotta forzatamente
dall'host remoto"}
Message: "Unable to read data from the transport connection:
Connessione in corso interrotta forzatamente dall'host remoto."
Source: "System"

As you can see the data of the error are different only in the description
but:
1) The description is in italian, but I could find a different
configuration
2) Generally speaking I think that is not a good idea to do test on strings

So I'd like to know if there is the possibility to get an unique error code
that is different in these two situations.

Thanks in advance

Starky




Peter Duniho replied on 18-May-07 02:03 PM
It depends on the exception.  I've found that in some cases, exceptions
that originate with some sort of lower-level system call do include an
error code.  You have to look at the internals of the exception type to
see if it's possible.  For example, the SocketException class includes an
ErrorCode property that corresponds to the underlying socket error.

In many cases, the best you can do is look at the type of the exception
itself.  For example, if you catch an IOException, then at least you know
that the exception wasn't caused (for example) by a null reference or out
of memory problem.  But you're right, it may be hard to narrow it down
farther than that.  Note that even in the IOException case, there are
derived exceptions that are more specific.  So if you catch the more
specific type, that can sometimes offer more detail (though in the
examples you gave, it doesn't look like that would be the case).

I also note that in IOException (and other System exceptions), there is an
HResult member that corresponds to the system error code.  However, it's
protected and unless you derive a new exception from the class, I don't
know how you'd get at that in any useful way (and if you're not throwing
the exception yourself, how would you derive from it? :) ).  You can look
at it in the debugger though.  :)

Pete
Starsky replied on 21-May-07 05:01 AM
Il Fri, 18 May 2007 11:03:24 -0700, Peter Duniho ha scritto:

[cut]
Thanks for your answer,
I was convinced that using the HRESULT value was a very good idea (I have a
long experience with the Win32 C/C++ development) but ... look at this !

I pasted the two difference situations, using a custom exception class I
can access to the protected HRESULT value and...there is the same value :-(

[Situation A]
{"I/O error occurred."}
_className: Nothing
_COMPlusExceptionCode: -532459699
_data: {System.Collections.ListDictionaryInternal}
_dynamicMethods: Nothing
_exceptionMethod: {System.Reflection.RuntimeMethodInfo}
_exceptionMethodString: Nothing
_helpURL: Nothing
_HResult: -2146232800
_innerException: Nothing
_maybeFullPath: Nothing
_message: "I/O error occurred."
_remoteStackIndex: 0
_remoteStackTraceString: Nothing
_source: "Server"
_stackTrace: {System.Array}
_stackTraceString: Nothing
_xcode: -532459699
_xptrs: 0
Data: {System.Collections.ListDictionaryInternal}
ExceptionCode: -2146232800
ExceptionDescr: "Unknown error (0x80131620)"
HelpLink: Nothing
HResult: -2146232800
InnerException: Nothing
IOEx: {"Unable to read data from the transport connection: Connessione
in corso interrotta forzatamente dall'host remoto."}
IsTransient: False
Message: "I/O error occurred."
Source: "Server"
TargetSite: {System.Reflection.RuntimeMethodInfo}

[Situation B]
{"I/O error occurred."}
_className: Nothing
_COMPlusExceptionCode: -532459699
_data: Nothing
_dynamicMethods: Nothing
_exceptionMethod: Nothing
_exceptionMethodString: Nothing
_helpURL: Nothing
_HResult: -2146232800
_innerException: Nothing
_maybeFullPath: Nothing
_message: "I/O error occurred."
_remoteStackIndex: 0
_remoteStackTraceString: Nothing
_source: Nothing
_stackTrace: {System.Array}
_stackTraceString: Nothing
_xcode: -532459699
_xptrs: 0
Data: {System.Collections.ListDictionaryInternal}
ExceptionCode: -2146232800
ExceptionDescr: "Unknown error (0x80131620)"
HelpLink: Nothing
HResult: -2146232800
InnerException: Nothing
IOEx: {"Unable to read data from the transport connection: Operazione
di I/O terminata a causa dell'uscita dal thread oppure della richiesta di
un'applicazione."}
IsTransient: False
Message: "I/O error occurred."
Source: "Server"
TargetSite: {System.Reflection.RuntimeMethodInfo}

Thanks for any suggestion...

Starsky
Peter Duniho replied on 21-May-07 12:58 PM
Well, if the HRESULT for both exceptions is the same, then you can't
really do much about that.  MSDN says that the error you're seeing here
("0x80131620") is the base exception code used for IOException.  And the
exceptions you posted don't have an "InnerException" that might give you
more detailed information.

In other words, that's all you're apparently going to get.  :(  At least,
that's how I read the docs (I don't have enough .NET experience to know
much of anything that's not in the documentation :) ).

I don't really know what sorts of things you'd do differently depending on
the specific exception, but you might think about other ways to
distinguish the situation.  For example, paying closer attention to which
function you've called that generated the exception, or something else
along those lines.

Pete