Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug this first chance exception on a Socket?

Tags:

c#

.net

sockets

wpf

In my C#/WPF/.Net 4.5 program I'm trying to do a Disconnect() on a socket and when I do I get a

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

. . . in the Visual Studio Output window.

My code is wrapped in a try/catch but it never lands in the catch . . .

    try
      {
          if (_TCPConn.Connected)
          {
              _TCPConn.Shutdown(SocketShutdown.Both);
              _TCPConn.Disconnect(true);   // SocketException
          }
      }
      catch (Exception e)
      {
   . . . 
      }

Up until this point the Socket works fine - it's used to communicate with some manufacturing machinery and all that stuff works perfectly. I've tried this with and without the Shutdown() with the same results. If I let my program keep running after the first chance exception the whole program just disappears and exits the debugger. Then the message in the Output window just says

The program '[0xE6C] ProcFacTest.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

How do I figure out what's causing this?

like image 748
user316117 Avatar asked Jan 08 '14 20:01

user316117


1 Answers

The first step is to disable "Just My Code". This probably shouldn't be necessary but it's possible the IDE is confusing your user code with system code and suppressing the exception as a result. I generally do this when doing exception debugging

  • Tools -> Options -> Debugging
  • Uncheck "Enable Justy My Code Debugging"

The next step is to tell Visual Studio to break when that particular exception is thrown.

  • Debug -> Exceptions
  • Expand "Common Language Runtime Exceptions" until you reach "System.Net.Sockets.SocketException"
  • Check "Thrown" for this exception
like image 149
JaredPar Avatar answered Oct 05 '22 12:10

JaredPar