ora*_*dov 19 resharper exception-handling try-catch rethrow
我认为抛出异常是一种很好的做法,让它冒泡回到用户界面或记录异常的地方并通知用户.
为什么resharper说这是多余的?
try
{
File.Open("FileNotFound.txt", FileMode.Open);
}
catch
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
yfe*_*lum 56
因为
try {
File.Open("FileNotFound.txt", FileMode.Open);
} catch {
throw;
}
Run Code Online (Sandbox Code Playgroud)
没有什么不同
File.Open("FileNotFound.txt", FileMode.Open);
Run Code Online (Sandbox Code Playgroud)
如果调用File.Open(string, FileMode)
失败,那么在任一示例中,完全相同的异常将找到通向UI的方式.
在catch
上面的那个子句中,您只是在不执行任何其他操作的情况下捕获并重新抛出异常,例如日志记录,回滚事务,包装异常以向其添加其他信息,或者根本不做任何事情.
然而,
try {
File.Open("FileNotFound.txt", FileMode.Open);
} catch(Exception ex) {
GetLogger().LogException(ex);
throw;
}
Run Code Online (Sandbox Code Playgroud)
不会包含任何裁员,ReSharper不应该抱怨.同样,
try {
File.Open("FileNotFound.txt", FileMode.Open);
} catch(Exception ex) {
throw new MyApplicationException(
"I'm sorry, but your preferences file could not be found.", ex);
}
Run Code Online (Sandbox Code Playgroud)
不会多余.
Otá*_*cio 17
因为上面的语句具有相同的行为,就好像它不存在一样.与写作相同:
File.Open("FileNotFound.txt", FileMode.Open);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5894 次 |
最近记录: |