Exception 是否在捕捉块下方(关闭后)编码?
我有一段代码,查看网络共享。一个错误是UnauthorizedAccessException,如果我需要使用basic auth登录以访问共享,就会发生此错误 因为如果您在异常出现时弹出窗口时登录,这个问题就会消失,所以这是否属于可以处理的异常?此外,写以下内容是否是一个好主意:Exception 是否在捕捉块下方(关闭后)编码?,exception,exception-handling,Exception,Exception Handling,我有一段代码,查看网络共享。一个错误是UnauthorizedAccessException,如果我需要使用basic auth登录以访问共享,就会发生此错误 因为如果您在异常出现时弹出窗口时登录,这个问题就会消失,所以这是否属于可以处理的异常?此外,写以下内容是否是一个好主意: string[] directories = null; try { directories = Dir
string[] directories = null;
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)
}
…这里有更多代码
或者,在try块中插入所有代码。我的印象是,如果异常是可以恢复的,那么catch块下面就可能有代码。构造这样的代码的最佳方法是什么?在我看来,try/catch块是一个很好的指示器,表明内部的代码正在执行一个“操作”,因此,在捕获之后放置更多的内容往往会导致打破方法的单一责任理论 我经常看到有2、3、4个堆叠的try/catch块的方法,这总是表明代码做得太多了