I have a snippet of code which looks at a network share. One error is UnauthorizedAccessException, which occurs if I need to sign in with basic auth to access the share.
Because this problem goes away if you sign in when the popup comes up at the time of the exception, does this fall under an exception which can be handled? Furthermore, would it be a good idea to write:
string[] directories = n开发者_如何学编程ull;
try
{
directories = Directory.GetDirectories(path);
}
catch (UnauthorizedAccessException unauthorizedAccessException)
{
Logger.Error(unauthorizedAccessException.Message, unauthorizedAccessException);
MessageBox.Show("An error has occur. Please check that you do not need to sign in to access the share. " + unauthorizedAccessException.Message, UiConstants.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
}
...MORE CODE HERE...
or, in the try block, insert all of the code. I am under the impression that if the exception can be recoverable, then there can be code below the catch block. What's the best way to structure such code?
In my opinion a try/catch block is a very good indicator that the code inside is doing one single 'action', and thus, putting more stuff after the catch tends to lead you to break the single responsibility theory of a method.
I often see methods that have 2, 3, 4 stacked try/catch blocks, and that is always a sign that that code is doing too much.
精彩评论