简体   繁体   English

捕获全局异常是处理重新连接的好方法吗?

[英]Is catching a global exception a good way to handle reconnecting?

I have a growing application with multiple usercontrols, windows, etc that all send a lot of data across a socket that's established when the program is opened (signing into a server).我有一个不断增长的应用程序,它有多个用户控件,windows 等,它们都通过打开程序(登录服务器)时建立的套接字发送大量数据。 If the server ever dies or something along those lines, I'd like to be able to handle the ensuing SocketException in a more graceful way than wrapping pretty much every code block in try-catch statements for that specific exception.如果服务器死机或发生类似情况,我希望能够以更优雅的方式处理随后的 SocketException,而不是将几乎每个代码块都包装在 try-catch 语句中以解决该特定异常。

void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)

Using that should catch any unhandled exception that gets thrown, would it be a bad idea to simply check if the exception is a socketexception and then launch the "you have disconnected, please reconnect" code?使用它应该捕获任何抛出的未处理异常,简单地检查异常是否是 socketexception 然后启动“您已断开连接,请重新连接”代码是不是一个坏主意? With this application, you can safely assume that 99% of any socketexception thrown would be because of a server disconnect.使用此应用程序,您可以安全地假设抛出的任何 socketexception 的 99% 都是由于服务器断开连接造成的。

And while I'm on the subject, any idea how you check the exception type after it's already caught?当我谈到这个主题时,知道在异常类型已经被捕获后如何检查它吗? With the above code, I can get the original exception simply with e.Exception.通过上面的代码,我可以简单地使用 e.Exception 获取原始异常。 I'm not really sure how to word an if statement to check the type of an exception.我不太确定如何用 if 语句来检查异常类型。

IMHO this is a very bad idea...恕我直言,这是一个非常糟糕的主意......

A better solution would be to move the code accessing the socket into its own class... anything in the application needing to interact with the server goes through that class... this way any socket-related exception can be handled accordingly in a central location.更好的解决方案是将访问套接字的代码移动到它自己的 class 中...应用程序中需要与服务器交互的任何内容都通过该 class...这样任何与套接字相关的异常都可以在中央处理地点。

Using that should catch any unhandled exception that gets thrown

Catching all the exceptions, where you can do little is not a good idea.捕捉所有的异常,你无能为力的地方不是一个好主意。

Eric Lippert has a very good article on what exception is handled. Eric Lippert 有一篇关于异常处理的非常好的文章。

I agree with Yahia that code accessing Socket moved to its own class.我同意 Yahia 的观点,访问 Socket 的代码移到了它自己的 class。

public class SocketHandling
{

 //if Something goes wrong, throw exception. It is up to the caller how to handle the exception.
}

main()
{
  try
{

}

catch(SocketRelatedException)
{
  //handle the exception.
}
}

I dont think it is bad practice to check the exception type.我不认为检查异常类型是不好的做法。

catch(Exception ex) 
            {
            if (ex is System.Net.Sockets.SocketException)
                {
                    doSomething(ex);
                }
            else 
              throw;

            }

Also, you can just catch a specific exception type...此外,您可以只捕获特定的异常类型......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM