这是我的代码(Javascript):
function getNumbers(){
var numberString = document.getElementById("numbers").value;
var actualNumbers = [];
var flowIndex = 0;
for(let i = 0; i < numberString.length; i++){
if(numberString.charAt(i) != " " && numberString.charAt(i) != "\," && numberString.charAt(i) != "\t"){
actualNumbers[flowIndex] = parseInt(numberString.charAt(i));
flowIndex++;
}
else continue;
}
return actualNumbers;
}
function division(){
try{
var answer = getNumbers()[0] / getNumbers()[1];
if(getNumbers()[1] == 0)
throw "bad";
}
catch(error){
throw error.description + "Division by zero error";
}
finally{
return answer;
}
}
我有一个函数
getNumbers()
,它返回一个包含array[0] = 1
和array[1] = 0
的数组。现在,我想在array[1] == 0
时抛出一个异常“bad”。但是,既没有引发try
异常,也没有引发catch
异常,但是finally子句正在运行。问题是什么?注意:除以零时,不会引发异常,而是答案是
Infinity
。 getNumbers()正常工作。 最佳答案
异常被抛出,但是您通过执行以下操作来抑制该异常:
finally {
return answer;
}
finally
子句拥有最终决定权。如果从中使用return
,则可以抑制该异常并使该函数正常完成。解决该问题的一种方法是删除
finally
子句,然后将return answer;
放入try
中。只是FWIW,还有一些其他注释作为注释:
function getNumbers(){
var numberString = document.getElementById("numbers").value;
var actualNumbers = [];
var flowIndex = 0;
// You might consider splitting the string into an array of one-character strings so you
// aren't constantly calling a method (`charAt`), like this:
// `var chars = numberString.split("");`
// Then index into `chars`
// N.B. `let` is an ES2015 (ES6) feature not all JavaScript engines have as it's new;
// the rest of your code is using the older `var`
// --v
for (let i = 0; i < numberString.length; i++){
// No need to escape the comma --------------------------------v
if(numberString.charAt(i) != " " && numberString.charAt(i) != "\," && numberString.charAt(i) != "\t"){
actualNumbers[flowIndex] = parseInt(numberString.charAt(i));
flowIndex++;
}
// No need for the `else continue;` at all
else continue;
// In the above, you regularly call `charAt` four times when once would have been sufficient.
// You might also consider a `switch`
}
return actualNumbers;
}
function division(){
try{
// Rather than calling `getNumbers` three separate times, call it once and remember its return value
// doing the calculation should be AFTER checking [1] for 0, not before
var answer = getNumbers()[0] / getNumbers()[1];
if(getNumbers()[1] == 0)
throw "bad"; // Recommend using Error, e.g.: `throw new Error("bad")`
// Move the `return answer;` here
}
catch(error){
// You've thrown a string, it doesn't have a `description` property
// Separately: Why throw something above, just to catch it here and throw something else?
throw error.description + "Division by zero error";
}
// Remove the finally
finally{
return answer;
}
}
还是FWIW,我可能要负责从
input
(甚至在调用division
的东西中)而不是division
中从getNumbers
获取值,并使用/\d/.test(...)
来测试字符是否为数字,因为有很多不是" "
,","
或"\t"
的非数字。并且一旦我们知道它们是数字,就可以使用+ch
而不是parseInt
进行转换(但是使用此输入,这只是一种样式选择[嗯,有性能暗示,但是99.99%的时间都没关系])。所以也许:
function getNumbers(str) {
var numbers = [];
str.split("").forEach(function(ch) {
if (/\d/.test(ch)) {
numbers.push(+ch);
}
});
return numbers;
}
function division() {
var numbers = getNumbers(document.getElementById("numbers").value);
if (numbers[1] == 0) {
throw new Error("Division by zero error");
}
return numbers[0] / numbers[1];
}
或者使用更简洁的备用
getNumbers
,但通过输入进行更多循环(通常无关紧要):function getNumbers(str) {
return str.split("")
.filter(function(ch) { return /\d/.test(ch); })
.map(function(ch) { return +ch; });
}
function division() {
var numbers = getNumbers(document.getElementById("numbers").value);
if (numbers[1] == 0) {
throw new Error("Division by zero error");
}
return numbers[0] / numbers[1];
}