Is the "message" of an exception culturally independent?

Posted by Ray Hayes on Stack Overflow See other posts from Stack Overflow or by Ray Hayes
Published on 2010-04-08T13:27:54Z Indexed on 2010/04/08 13:33 UTC
Read the original article Hit count: 173

Filed under:
|
|

In an application I'm developing, I have the need to handle a socket-timeout differently from a general socket exception. The problem is that many different issues result in a SocketException and I need to know what the cause was.

There is no inner exception reported, so the only information I have to work with is the message:

"A connection attempt failed because the connected party did not 
properly respond after a period of time, or established connection 
failed because connected host has failed to respond"

This question has a general and specific part:

  1. is it acceptable to write conditional logic based upon the textual representation of an exception?
  2. Is there a way to avoid needing exception handling?

Example code below...

try 
{
    IPEndPoint endPoint = null; 
    client.Client.ReceiveTimeout = 1000;
    bytes = client.Receive(ref endPoint);
}
catch( SocketException se )
{
    if ( se.Message.Contains("did not properly respond after a period of time") )
    {
        // Handle timeout differently..
    }
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#