错误处理
·
Yin灏
语法错误
let v01 = 100, v02 = 230;
let result = 0;
try {
eval('result = v01 -- v02 ;')
} catch(err) {
console.log(err); // SyntaxError: Unexpected identifier at index.js:6
console.log(typeof err); // 'object'
console.log(err instanceof SyntaxError); // true
console.log(err.name); // SyntaxError
console.log(err.message); // Unexpected identifier
console.log(err.stack); // SyntaxError: Unexpected identifier at index.js:6
}
数据类型错误
js
引擎无法顺利转换特定操作数据的数据类型时,便会产生数据类型的错误。
let v01 = 100, v02 = 230;
let result = 0;
try {
result = v01.replace('0', '1');
console.log(result);
} catch(err) {
with(console) {
log(err); // v01.replace is not a function
log(typeof err);
log(err instanceof TypeError); // true
log(err.name); // TypeError
log(err.message); // v01.replace is not a function
log(err.stack); // TypeError: v01.replace is not a function
}
}
评估错误
js
引擎现今只能通过内置函数或者语句,才会产生评估错误。
try {
throw new EvalError('Equation evaluation error occurs!');
} catch(err) {
with(console) {
log(err); // EvalError: Equation evaluation error occurs!
log('\n');
log(typeof err); // object
log(err instanceof EvalError); // true
log('\n');
log(err.name); // EvalError
log(err.message); // Equation evaluation error occurs!
log(err.stack); // EvalError: Equation evaluation error occurs!
}
}
范围错误
js
引擎评估到特定数据范围以外的数据时,便会产生数据范围错误。
var value = 200;
let list = null;
let result = 0;
try {
result = value.toPrecision(101);
list = new Array(-3);
console.log(result);
} catch(err) {
with(console) {
log(err); // RangeError: toPrecision() argument must be between 1 and 100 at Number.toPrecision (<anonymous>)
log('\n');
log(typeof err); // object
log(err instanceof RangeError); // true
log('\n');
log(err.name); // RangeError
log(err.message); // toPrecision() argument must be between 1 and 100
log(err.stack); // RangeError: toPrecision() argument must be between 1 and 100 at Number.toPrecision (<anonymous>)
}
}
引用错误
尚未被声明的变量名称,被引用,会产生引用错误。
let v01 = 100, v02 = 150;
let result = 0;
try {
result += v01 + v03;
console.log(result);
} catch(err) {
with(console) {
log(err); // ReferenceError: v03 is not defined
log('\n');
log(typeof err); // object
log(err instanceof ReferenceError); // true
log('\n');
log(err.name); // ReferenceError
log(err.message); // v03 is not defined
log(err.stack); // ReferenceError: v03 is not defined
}
}
网址在编码或解码上的错误
js
引擎无法顺利对特定网址进行编码会解码时,便会产生 URI 错误,引申为网址在编码或解码上的错误。
try {
decodeURIComponent('https://tw.dictionary.yahoo.com/dictionary?p=%E8%81%96%E8%AA%95%E7%AF%FF')
} catch(err) {
with(console) {
log(err); // URIError: URI malformed at decodeURIComponent (<anonymous>)
log('\n');
log(typeof err); // object
log(err instanceof URIError); // true
log('\n');
log(err.name); // URIError
log(err.message); // URI malformed
log(err.stack); // URIError: URI malformed at decodeURIComponent (<anonymous>)
}
}
逻辑错误
js
引擎无法察觉,编程人员思考不够周全导致的逻辑错误。
let num_list = Array.from(new Array(11), (value, index) => parseInt(100 * Math.random()));
let result = null;
result = Math.min(...num_list);
console.log(num_list);
console.log(result);
console.log(`The maximum value is ${result}.`);
let radius = 55;
result = (3 / 4 * Math.PI * radius ** 3).toFixed(3);
console.log(`The sphere volume of radius ${radius} is ${result}`);
处理特定异常错误
程序员重新定义错误,成为用户可以理解或不会看见的错误信息。
try {
// throw new URIError('Defined error message here...')
throw 'Logical error occurs';
} catch(err) {
console.log(err.name);
console.log(err.message);
console.log(err.stack);
} finally {
console.log('Message after exception handling.');
}
调试机制
js
引擎支持两种调试机制:
- 严格模式(
use strict
) debugger
严格模式
js
引擎处于严格模式下,对源码的语法要求是相当严格的。
"use strict";
try {
let v01 = 100;
var v02 = 250;
v03 = 600;
} catch(err) {
console.log(err); // ReferenceError: v03 is not defined
}
function fun01() {
radius = 10;
return 4 * Math.PI * radius ** 2;
}
try {
fun01();
} catch (err) {
console.log(err); // ReferenceError: radius is not defined
}
try {
eval('\
with(Math)\
{\
console.log(PI);\
}')
} catch (err) {
console.log(err); // SyntaxError: Strict mode code may not include a with statement
}
try {
let num01 = 0;
eval('num01 = 0377;')
} catch (err) {
console.log(err); // SyntaxError: Octal literals are not allowed in strict mode.
}
try {
let person = {name: 'Jasper', gender: 'male', age: '33'};
eval('delete person;')
} catch (err) {
console.log(err); // SyntaxError: Delete of an unqualified identifier in strict mode.
}
源代码的断点设置和逐句执行
let radius = 10, height = 15;
let result = 0;
result = Math.PI * (radius ** 2) * height;
console.log(result);
debugger;
result = 2 * Math.PI * radius ** 2 + 2 * Math.PI * radius * height;
console.log(result);
debugger;
result = 4 / 3 * Math.PI * radius ** 3;
console.log(result);
debugger;
result = 4 * Math.PI * radius ** 2;
console.log(result);