js错误类型及收集

220 阅读1分钟

错误类型

一、编译时错误(语法)

无法捕获!整个js文件无法执行

1、SyntaxError

语法错误,阻塞js执行 Uncaught SyntaxError: Invalid or unexpected token

const 1d = 'lili'

二、运行时错误(异常)

可以捕获(异步不行),出错行之后的代码无法执行

1、TypeError

Uncaught TypeError: Assignment to constant variable.

const a = 1;
a = 2;

Uncaught TypeError: b.reverse is not a function

const a = 3;
a.reverse();

Uncaught TypeError: Cannot read property 'index' of undefined

const c = {name: 'jj', age: 8};
console.log(c.like.index);

2、ReferenceError

Uncaught ReferenceError: f is not defined

const e = f

3、RangeError

Uncaught RangeError: Invalid array length

const g = [1,2,3]
g.length = -5

4、URIError

Uncaught URIError: URI malformed

decodeURI("%")

5、EvalError

?

6、抛异常未捕获

Uncaught Error: I am error

throw new Error('I am error')

www.jianshu.com/p/467b9a145… docs.w3cub.com/javascript/…

收集错误

1、 window.onerror

window.onerror = function(errorMsg, url, lineNumber, columnNumber, errorObj) {
    arguments // ["Script error.", "", 0, 0, null, callee: ƒ, Symbol(Symbol.iterator): ƒ]
}

TODO

blog.fundebug.com/2019/07/06/…