Why doesn't this SocketException get into the general signing procedure?

Our company provides a network component (DLL) for a GUI application.

It uses a timer that checks for disconnections. If he wants to reconnect, he calls:

internal void timClock_TimerCallback(object state) { lock (someLock) { // ... try { DoConnect(); } catch (Exception e) { // Log e.Message omitted // Raise event with e as parameter ErrorEvent(this, new ErrorEventArgs(e)); DoDisconnect(); } // ... } } 

So the problem is that inside the DoConnect () procedure, a SocketException is thrown (and not caught). I would suggest that catch (Exception e) should catch ALL exceptions, but somehow the SocketException was not detected and is displayed in the GUI application.

 protected void DoConnect() { // client = new TcpClient(); client.NoDelay = true; // In the following call the SocketException is thrown client.Connect(endPoint.Address.ToString(), endPoint.Port); // ... (login stuff) } 

Doc has confirmed that SocketException extends the exception. Found stack appeared:

 TcpClient.Connect() -> DoConnect() -> timClock_TimerCallback 

Thus, an exception is not thrown outside the try / catch block.

Any ideas why this is not working?

+4
source share
4 answers

I wrote a small program and could not reproduce, SocketException was caught inside TimerCallback just fine.

Therefore, I suggest you reconsider your analysis, the problem may not be what you think. A few tips:

  • run it outside the timer. T | hat takes the thread out of the loop.
  • run it in the debugger. Where does the exception really happen?
  • perform exception handling. Is ErrorEvent doing what it needs?
0
source

If the ErrorEvent does raise another exception (for comment), then DoDisconnect() never executed.

Otherwise, the exception you see may appear in the form DoDisconnect()

+1
source

Could you post the DoConnect () code?

You should also try: Can you catch it in DoConnect ()? Try to catch a specific exception instead of a simple one. How does it react if you use debug mode?

0
source

Your timClock_TimerCallback not called on the same thread as the catch-statement that wants to catch the exception. You have to catch the exception inside timClock_TimerCallback , and then call the method that calls itself, and then rebuild the exception in the correct thread.

Not sure if this will work, but you can try.

0
source

Source: https://habr.com/ru/post/1300645/


All Articles