4

I have a confusing problem and I could not any way to workaround it. I have a couple of lines of codes, and at some point, the Send function throws a System.Net.Sockets.SocketException. However, even though try has the catch block, it does not catches the exception and continue with normal flow. When I try to run the case where it needs to throw same exception, catch captures it pretty well.

Things I have tried:

  • Trying to change VS debugging settings 'Enable Only For My Code', but it is already disabled.
  • I tried to both Debug and Release modes.
  • I tried using general Exception catcher, but none of them worked.

Here is the code, at some point, the connection from the server closes and the c Socket disconnects. It returns the System.Net.Sockets.SocketException, but below catch does not capture it.

            try
            {
                // Some code here . . . 

                c.Send(clientData);
                Print("Started uploading the file " + uploadFilename + ".");
                upBox.Text = "";
                Print("Finished uploading the file " + uploadFilename + ".");
            }
            catch (System.Net.Sockets.SocketException)
            {
                Print("You are disconnected from server!");
            }
hevele
  • 903
  • 6
  • 20
  • 38
  • If nothing is catching the exception, how do you know it occurs? Do you get the "The program stopped working" thing from Windows? – Steve Nov 21 '13 at 02:26
  • I see the exception thrown from the by breakpointing and inspecting from the watch menu. In the watch panel, when I write c.send(clientData), I can see the exception rather than the number of bytes sent. – hevele Nov 21 '13 at 07:46
  • 1
    So the exception is happening while your program isn't running. That's the "some point" you said the exception was occurring at. Your program is not causing the exception. What you're trying to do might not be valid in that context. – Steve Nov 21 '13 at 13:26
  • Yes, my program isn't the source of exception, server is. Is there a way for resolving this and catch it? – hevele Nov 21 '13 at 14:26
  • 1
    But your code isn't throwing an exception, so why does this exception need to be fixed for your program? Why do you think this is the server, and not the fact that your at a breakpoint in your program and trying to send data over a socket in the watch window? Have you been able to do this before? (Full disclosure: Not that I know one way or the other, I've never tried it) – Steve Nov 21 '13 at 16:35
  • same problem here.this exception need to be fixed because my program will crashes after this Exception from server !!!! – Mamad May 01 '18 at 10:40

1 Answers1

1

The reason you're not getting an exception ... ... is that no exception has occurred!

TCP/IP itself won't necessarily "notice" a disconnect ... unless you explicitly try to write to a closed socket.

Definitely check out Beej's Guide to Network Programming:

See also:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I can see the exception thrown from the watch menu. Does that mean that send command have not thrown exception? – hevele Nov 21 '13 at 07:48
  • The send command *did* throw an exception, just not the call in the code above - but by the attempted execution of the method in the watch window. – Steve Nov 21 '13 at 13:28