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). 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.
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? With this application, you can safely assume that 99% of any socketexception thrown would be because of a server disconnect.
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. I’m not really sure how to word an if statement to check the type of an exception.
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.