JavaScript 异常处理未按预期工作
JavaScript Exception handling not working as expected
这是我第一次使用 JavaScript 异常和一般的异常处理。
好的,我有以下简单的代码。
function getMonth( month ) {
month = month -1;
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
if( month != null ) {
if( months.indexOf( month ) ) {
return months[month];
} else {
throw "Invalid number for month";
}
} else {
throw "No month provided";
}
}
基本上它首先检查是否为函数提供了参数,如果没有,则抛出异常。其次,它检查提供的数字是否匹配有效的月份数字,如果不匹配则抛出异常。
我运行它如下:
try {
month = getMonth();
} catch( e ) {
console.log( e );
}
控制台不记录异常,"No month provided"。为什么是这样?我目前正在阅读有关例外情况的信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#Exception_Handling_Statements
因为月份变量是undefined
而不等于null
。
try {
month = getMonth();
alert(month);
} catch( e ) {
alert(e);
}
function getMonth( month ) {
month = month -1;
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
if( month != null ) {
if( months.indexOf( month ) ) {
return months[month];
} else {
throw "Invalid number for month";
}
} else {
throw "No month provided";
}
}
有关详细信息,请阅读此 article
这是一个棘手的问题。这里的问题是:您的代码实际上从未到达 throw
语句,而是正常运行 returns。原因有点"JavaScriptish":
在调用该函数时,变量 month
被引入(因为它在参数中),但此时它的值为 undefined
。语句 month -1;
与 undefined - 1
相同,后者 return 是 Illegal Number
或 NaN
。 (如果它真的扔了会更好,我同意)。
您的第一个 if(month != null)
现在解析为 true
为 month = NaN
和 NaN != null
。第二个条件 return 为真,因为如果给定值在数组中为 NOT,则 Array.indexOf
实际上会 return -1
。 -1
是一个有效值,因此解析为 true
。
我再次同意 - 这有点违反直觉。但是,嘿 - 欢迎来到 JavaScript。它有好的一面,我保证 ;)
小提示:尝试我在浏览器控制台中发布的所有这些条件和语句,然后你会更好地理解它。
这是我第一次使用 JavaScript 异常和一般的异常处理。
好的,我有以下简单的代码。
function getMonth( month ) {
month = month -1;
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
if( month != null ) {
if( months.indexOf( month ) ) {
return months[month];
} else {
throw "Invalid number for month";
}
} else {
throw "No month provided";
}
}
基本上它首先检查是否为函数提供了参数,如果没有,则抛出异常。其次,它检查提供的数字是否匹配有效的月份数字,如果不匹配则抛出异常。
我运行它如下:
try {
month = getMonth();
} catch( e ) {
console.log( e );
}
控制台不记录异常,"No month provided"。为什么是这样?我目前正在阅读有关例外情况的信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#Exception_Handling_Statements
因为月份变量是undefined
而不等于null
。
try {
month = getMonth();
alert(month);
} catch( e ) {
alert(e);
}
function getMonth( month ) {
month = month -1;
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
if( month != null ) {
if( months.indexOf( month ) ) {
return months[month];
} else {
throw "Invalid number for month";
}
} else {
throw "No month provided";
}
}
有关详细信息,请阅读此 article
这是一个棘手的问题。这里的问题是:您的代码实际上从未到达 throw
语句,而是正常运行 returns。原因有点"JavaScriptish":
在调用该函数时,变量 month
被引入(因为它在参数中),但此时它的值为 undefined
。语句 month -1;
与 undefined - 1
相同,后者 return 是 Illegal Number
或 NaN
。 (如果它真的扔了会更好,我同意)。
您的第一个 if(month != null)
现在解析为 true
为 month = NaN
和 NaN != null
。第二个条件 return 为真,因为如果给定值在数组中为 NOT,则 Array.indexOf
实际上会 return -1
。 -1
是一个有效值,因此解析为 true
。
我再次同意 - 这有点违反直觉。但是,嘿 - 欢迎来到 JavaScript。它有好的一面,我保证 ;)
小提示:尝试我在浏览器控制台中发布的所有这些条件和语句,然后你会更好地理解它。