gpt4 book ai didi

javascript - 尝试使用 try 和 catch 来确定条件

转载 作者:行者123 更新时间:2023-11-28 03:39:16 25 4
gpt4 key购买 nike

我有一个检查版本的方法。现在,如果文件版本不存在,我想通过 try and catch 显示此错误

我正在尝试这样做,但不知道这是否好

  public checkVersionFile(): any {
const contentVersion = fs.readFileSync( "Version", 'utf8')

try{
if(contentVersion.endsWith('-SNAPSHOT')){
let match = /^(\d+)\.(\d+)\.(\d+)-(\d+)-(SNAPSHOT)$/.exec(contentVersion);
const version = (parseInt(match[1])) + '.' + (parseInt(match[2])) + '.' + (parseInt(match[3])) + '-' + (parseInt(match[4]));
return version
}
else {
logger.error("Cannot be released without SNAPSHOT inside Version file in components");
}
}
catch (err) {
return Promise.reject("file Version doesn't exist");
}

}

感谢您的帮助:)

最佳答案

文件读取操作不在 try/catch block 内,因此无法捕获此错误。

  public checkVersionFile(): any {


try {
const contentVersion = fs.readFileSync( "Version", 'utf8')

if(contentVersion.endsWith('-SNAPSHOT')){
let match = /^(\d+)\.(\d+)\.(\d+)-(\d+)-(SNAPSHOT)$/.exec(contentVersion);
const version = (parseInt(match[1])) + '.' + (parseInt(match[2])) + '.' + (parseInt(match[3])) + '-' + (parseInt(match[4]));
return version
}
else {
logger.error("Cannot be released without SNAPSHOT inside Version file in components");
}
}
catch (err) {
return Promise.reject("file Version doesn't exist");
}

}

关于javascript - 尝试使用 try 和 catch 来确定条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57431336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com