【发布时间】:2015-03-26 13:00:39
【问题描述】:
我有一个使用 3 个变量和一个 try catch 的函数。我想将这 3 个变量用作数组的一部分,但如果我这样做了,我将无法正确完成 try catch 和函数
JS:
function calculateVolume() {
length = document.getElementById("Length").value;
width = document.getElementById("Width").value;
height = document.getElementById("Height").value;
try {
if ((length > 1000) || (width > 1000) || (height > 1000))
throw "err"
else
var volume = length * width * height;
alert(volume);
} catch (err) {
alert("You have not entered all three dimensions correctly");
}
}
【问题讨论】:
-
你遇到了什么错误?
-
您的代码运行良好。究竟是什么问题?
-
请注意:虽然可以在 js 中将任何你想要的东西作为错误抛出,但你仍然应该使用真正的 Error 对象。您可能想阅读answer 到问题Throwing strings instead of Errors
-
@tymeJV 可能你不知道,但在 js 中 var_hoisting
-
数组在哪里出现 - 我没有看到它?长度、宽度和高度是否意味着在一个数组中?
标签: javascript arrays variables try-catch