为什么这个 SocketException 没有被通用 catch 例程捕获?

发布于 2024-08-20 11:36:48 字数 1028 浏览 10 评论 0原文

我们公司为 GUI 应用程序提供网络组件 (DLL)。

它使用计时器来检查断开连接。如果它想重新连接,它会调用:

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();
    }
    // ...
  }
}

所以问题是,在 DoConnect() 例程内部抛出了 SocketException(并且未被捕获)。我认为,catch(异常 e)应该捕获所有异常,但不知何故,SocketException 未被捕获并显示在 GUI 应用程序中。

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)
}

该文档确认 SocketException 扩展了 Exception。 显示的堆栈跟踪是:

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

因此,异常不会抛出到 try/catch 块之外。

有什么想法为什么它不起作用吗?

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

It uses a Timer that checks for disconnections. If it wants to reconnect, it 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, inside of the DoConnect() routine a SocketException is thrown (and not caught). I would assume, that the catch (Exception e) should catch ALL exceptions but somehow the SocketException was not caught and shows up to 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)
}

The doc confirmed that SocketException extends Exception.
The stacktrace that showed up is:

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

So the exception is not thrown outside the try/catch block.

Any ideas why it doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自演自醉 2024-08-27 11:36:48

如果 ErrorEvent 确实引发了另一个异常(根据注释),则永远不会执行 DoDisconnect() 。

否则,您看到的异常可能来自 DoDisconnect()

If ErrorEvent really raises another exception (per the comment), then DoDisconnect() is never executed.

Otherwise, the exception you see might be coming form DoDisconnect()

夏至、离别 2024-08-27 11:36:48

我写了一个小程序,但无法重现,在 TimerCallback 中捕获了 SocketException 就好了。

所以我建议你重新思考你的分析,问题可能不是你想象的那样。一些建议:

  • 在计时器之外运行它。这使得线程脱离了循环。
  • 在调试器中运行它。异常真正发生在哪里?
  • 单步执行异常处理。 ErrorEvent 是否做了它应该做的事情?

I wrote a little program and was unable to reproduce, a SocketException was caught inside a TimerCallback just fine.

So I suggest you re-think your analysis, the problem may not be what you think it is. A few suggestions:

  • run it outside the Timer. T|hat takes the threading out of the loop.
  • run it in the debugger. Where does the exception really occur?
  • step through the exception handling. Is ErrorEvent doing what it should?
空气里的味道 2024-08-27 11:36:48

你能发布 DoConnect() 代码吗?

还有值得尝试的事情:
你能在 DoConnect() 中捕获它吗?
尝试捕获特定的异常,而不仅仅是一般的异常。
如果您使用调试模式,它会如何反应?

Could you post the DoConnect() code?

Also things to try:
Can you catch it in the DoConnect()?
Try catching the specific exception instead of just the generic.
How does it react if you use debug mode?

婴鹅 2024-08-27 11:36:48

您的 timClock_TimerCallback 不会在 catch 语句想要捕获异常的同一线程中调用。您应该在 timClock_TimerCallback 中捕获异常,然后调用一个调用自身的方法,然后在正确的线程中重新抛出异常。

不确定这是否有效,但你可以尝试一下。

Your timClock_TimerCallback isn't called in the same thread as the catch-statement wants to catch an exception. You should catch the exception inside timClock_TimerCallback and then call a method which invokes itself and then rethrow the exception in the right thread.

Not sure this will work, but you could give it a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文