为什么这个 SocketException 没有被通用 catch 例程捕获?
我们公司为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 ErrorEvent 确实引发了另一个异常(根据注释),则永远不会执行 DoDisconnect() 。
否则,您看到的异常可能来自
DoDisconnect()
If
ErrorEvent
really raises another exception (per the comment), thenDoDisconnect()
is never executed.Otherwise, the exception you see might be coming form
DoDisconnect()
我写了一个小程序,但无法重现,在 TimerCallback 中捕获了 SocketException 就好了。
所以我建议你重新思考你的分析,问题可能不是你想象的那样。一些建议:
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:
你能发布 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?
您的
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 insidetimClock_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.