关闭文件流时添加try/catch

Signed-off-by: 陈怡博 <chenyibo6@huawei.com>
This commit is contained in:
陈怡博 2024-06-11 11:14:22 +08:00
parent bf9b167ed0
commit 1908ccb73d

View File

@ -20,7 +20,7 @@ import { BusinessError } from '@ohos.base';
export class FileIoModel {
getMediaFileData(mediaUri: string, callback: Function): void {
console.log('CertManager FA getMediaFile start');
let file : fs.File | undefined = undefined;
let file: fs.File | undefined = undefined;
try {
file = fs.openSync(mediaUri, fs.OpenMode.READ_ONLY);
let stat = fs.statSync(file.fd);
@ -33,8 +33,13 @@ export class FileIoModel {
console.error('CertManager FA getMediaFileData failed with err, message: ' + e.message + ', code: ' + e.code);
callback(undefined);
} finally {
if (file !== undefined && file !== null) {
fs.closeSync(file.fd);
try {
if (file !== undefined && file !== null) {
fs.closeSync(file.fd);
}
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error('CertManager FA close io stream failed with err, message: ' + e.message + ', code: ' + e.code);
}
}
}