在执行JavaScript代码的时候,有些情况下会发生错误。
第一类是语法错误,此类错误会导致整个js文件无法执行。另一类错误统称为异常,例如,网络连接中断,读取不存在的文件,没有操作权限等;此类错误不影响错误前的代码执行。对于第一种错误,要修复程序。对于第二种错误,我们需要处理它,并可能需要给用户反馈。
目录
使用错误处理逻辑:
try ... catch ... finally
var r1, r2, s = null;
try {
r1 = s.length; // 此处应产生错误
r2 = 100; // 该语句不会执行
} catch (e) {
console.log('出错了:' + e);
} finally {
console.log('finally');
}
console.log('r1 = ' + r1); // r1应为undefined
console.log('r2 = ' + r2); // r2应为undefined
当代码块被try { ... }
包裹的时候,就表示这部分代码执行过程中可能会发生错误,一旦发生错误,就不再继续执行后续代码,转而跳到catch
块。catch (e) { ... }
包裹的代码就是错误处理代码,变量e
表示捕获到的错误。最后,无论有没有错误,finally
一定会被执行。
有错误发生时,执行流程像这样:
- 先执行
try { ... }
的代码; - 执行到出错的语句时,后续语句不再继续执行,转而执行
catch (e) { ... }
代码; - 最后执行
finally { ... }
代码。
没有错误发生时,执行流程像这样:
- 先执行
try { ... }
的代码; - 因为没有出错,
catch (e) { ... }
代码不会被执行; - 最后执行
finally { ... }
代码。
注意,catch
和finally
可以不必都出现。也就是说,try
语句一共有三种形式:
完整的try ... catch ... finally:
try {
...
} catch (e) {
...
} finally {
...
}
只有try ... catch,没有finally:
try {
...
} catch (e) {
...
}
只有try ... finally,没有catch:
try {
...
} finally {
...
}
错误对象(标准内置对象之一)
JavaScript有一个标准的Error
对象表示错误。
常见6大错误类型
以下的6种派生错误,连同原始的Error对象,都是构造函数。开发者可以使用它们,认为生成错误对象的实例。
new Error([message[,fileName[,lineNumber]]])
第一个参数表示错误提示信息,第二个是文件名,第三个是行号。
1.SyntaxError:语法错误
解析代码时,发现存在语法错误就抛出这个错误.
// 1. Syntax Error: 语法错误
// 1.1 变量名不符合规范
var 1 // Uncaught SyntaxError: Unexpected number
var 1a // Uncaught SyntaxError: Invalid or unexpected token
// 1.2 给关键字赋值
function = 5 // Uncaught SyntaxError: Unexpected token =
2.ReferenceError:引用错误
// 2.1 引用了不存在的变量
a() // Uncaught ReferenceError: a is not defined
console.log(b) // Uncaught ReferenceError: b is not defined
// 2.2 给一个无法被赋值的对象赋值
console.log("abc") = 1 // Uncaught ReferenceError: Invalid left-hand side in assignment
3.RangeError:范围错误
RangeError是当一个值超出有效范围时发生的错误。主要的有几种情况,第一是数组长度为负数,第二是Number对象的方法参数超出范围,以及函数堆栈超过最大值。
// 3.1 数组长度为负数
[].length = -5; // Uncaught RangeError: Invalid array length
var a= new Array(-1); // Uncaught RangeError: invalid array length
// 3.2 Number对象的方法参数超出范围
var num = new Number(12.34)
console.log(num.toFixed(-1)) /* Uncaught RangeError: toFixed() digits argument
must be between 0 and 20 at Number.toFixed */
// 说明: toFixed方法的作用是将数字四舍五入为指定小数位数的数字,参数是小数点后的位数,范围为0-20.
4.TypeError:类型错误
变量或参数不是预期类型时发生的错误。比如使用new字符串、布尔值等原始类型和调用对象不存在的方法就会抛出这种错误,因为new命令的参数应该是一个构造函数。
// 4.1 调用不存在的方法
123() // Uncaught TypeError: 123 is not a function
var o = {}
o.run() // Uncaught TypeError: o.run is not a function
// 4.2 new关键字后接基本类型
var p = new 456 // Uncaught TypeError: 456 is not a constructor
5.URIError:URL错误
主要是相关函数的参数不正确。URI相关参数不正确时抛出的错误,主要涉及6个函数:encodeURI()、decodeURI()、encodeURIComponent()、escape()、unescape()。
decodeURI("%") // Uncaught URIError: URI malformed at decodeURI
6.EvalError:eval()函数执行错误
在ES5以下的JavaScript中,当eval()函数没有被正确执行时,会抛出evalError错误。ES5以上的JavaScript中已经不再抛出该错误,但依然可以通过new关键字来自定义该类型的错误提示。
throw
语句抛出错误
通过new关键字来自定义该类型的错误提示。同时让程序主动抛出一个错误,让执行流程直接跳转到catch
块。
var r, n, s;
try {
s = prompt('请输入一个数字');
n = parseInt(s);
if (isNaN(n)) {
throw new Error('输入错误'); // 定义一个错误,并且让程序主动抛出
}
// 计算平方:
r = n * n;
console.log(n + ' * ' + n + ' = ' + r);
} catch (e) {
console.log('出错了:' + e);
}
Error的传播路径
如果在一个函数内部发生了错误,它自身没有捕获,错误就会被抛到外层调用函数,如果外层函数也没有捕获,该错误会一直沿着函数调用链向上抛出,直到被JavaScript引擎捕获,代码终止执行。所以,我们不必在每一个函数内部捕获错误,只需要在合适的地方来个统一捕获:
'use strict';
function main(s) {
console.log('BEGIN main()');
try {
foo(s);
} catch (e) {
console.log('出错了:' + e);
}
console.log('END main()');
}
function foo(s) {
console.log('BEGIN foo()');
bar(s);
console.log('END foo()');
}
function bar(s) {
console.log('BEGIN bar()');
console.log('length = ' + s.length);
console.log('END bar()');
}
main(null);
/*输出
BEGIN main()
BEGIN foo()
BEGIN bar()
出错了:TypeError: s is null
END main()
*/
当bar()
函数传入参数null
时,代码会报错,错误会向上抛给调用方foo()
函数,foo()
函数没有try ... catch语句,所以错误继续向上抛给调用方main()
函数,main()
函数有try ... catch语句,所以错误最终在main()
函数被处理了。至于在哪些地方捕获错误比较合适,需要视情况而定。