Catching Specific Socket Exceptions [CS/C# 2005]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I have a main application [frmMain] that launches threads (tcp listeners) - at a certain point I want to STOP these threads (which will close my listener, sockets, and abort the thread - this all works fine).
Thing is it also launches 2 exceptions:
- SocketException: A blocking operation was interrupted by a call to WSACancelBlockingCall
- ThreadAbortException: Thread was being aborted.

This is my current CATCH-phase code:
Code:
catch (SocketException sex)
{ Log.Trace("Socket Exception: " + sex); }
catch (Exception ex)
{ Log.Trace("General Exception: " + ex); }

Now I am pretty sure I can catch the Thread Abort Exception using: "catch (ThreadAbortException) {}" first on the list...
But I have no clue how to "catch" the specific Socket Exception ("A blocking operation was interrupted by a call to WSACancelBlockingCall") so that it does NOT log as an exception (it is perfectly fine that it was interrupted by me when aborting the thread), not as simple as ThreadAbort... How can I determine if this is NOT an exception I want to log?

Any ideas, hints, and help would be greatly appreciated, thanks
 
Have you checked the ErrorCode property for the SocketException class? It is an integer and is probably unique the specific type of exception you are looking for. If so you could do something like this:
C#:
try
{
}
catch (SocketException ex)
{
    if ( ex.ErrorCode == 100 )
    {
        // handle
    }
    else
    {
        throw ex;
    }
}
 
Back
Top