在Javascript中如何使用Python中的Try Except逻辑? 即,我在括号中放了什么?这是Python中Try Except逻辑的一个例子。 如何转换为Javascript?
num = input("Please enter a number: ")
try:
int(num)
except(ValueError):
print("Character not a number.")
num = input("Please enter a number: ")
因此程序将尝试将变量转换为整数,但如果发生错误,则提醒用户该错误,并重新请求变量num的值。 请将它转换为Javascript吗?
答案 0 :(得分:1)
JavaScript知道try-catch中的等价物
try {
try_statements
} catch (condition_1) {
catch_statements_1
}
文档here
示例:
try {
throw 'myException'; // generates an exception
} catch (e) {
// statements to handle any exceptions
console.log(e) // log error to console
}
因此,当try块中发生异常时,它是catch
- 由catch块执行,并且将执行该块中的代码语句。