Ted*_*opp 7 java eclipse warnings static-analysis resource-leak
Eclipse(Juno)发出以下警告:
潜在的资源泄漏:'os'可能不会被关闭
try在此代码中正文的第一行:
static void saveDetails(byte[] detailsData) {
OutputStream os = null;
try {
os = sContext.openFileOutput(DETAILS_FILE_NAME, Context.MODE_PRIVATE);
os.write(detailsData);
} catch (IOException e) {
Log.w(LOG_TAG, "Unable to save details", e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException ignored) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
openFileOutput声明该方法抛出一个FileNotFoundException.
这是误报吗?这似乎是一个相当普通的执行路径分析.
Oli*_*ier 11
在我看来,这是一个误报.您的资源在"finally"块中关闭,因此我无法看到这里可能出现的问题.
作为旁注,如果您使用的是Java 7,我建议使用"try-with-resources"习惯用法.
static void saveDetails(byte[] detailsData) {
try (OutputStream os = sContext.openFileOutput(DETAILS_FILE_NAME, Context.MODE_PRIVATE);) {
os = sContext.openFileOutput(DETAILS_FILE_NAME, Context.MODE_PRIVATE);
os.write(detailsData);
} catch (IOException e) {
Log.w(LOG_TAG, "Unable to save details", e);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4738 次 |
| 最近记录: |