js Error

150 阅读1分钟

自定义Error

image.png

const myError = new Error('ooopie')

myError.name  //"Error"
myError.message   //"ooopie"
myError.stach   //它发生在一个匿名函数的内部,该函数是主要的全局执行上下文你
/*It happened inside of an anonymous function,which is the main global excution context*/


//示例
function a (){
    const b = new Error('what???')
    return b
}

a()

/*结果
Error: what???
    at a (<anonymous>:2:15)   //and then A function that was called ,a().stack错误原因
    at <anonymous>:6:1       //first item on the stack,which is the global anonymous global execution context and then 
*/

常规错误类型

image.png

image.png

image.png

Extending Errors

class authenticationError extends Error{
    constructor(message){
        super(message)
        this.name = 'authenticationError'
        this.favouriteSnack = 'grapes'
    }
}

const a = new authenticationError('oopsie')

a.favouriteSnack
sequenceDiagram
Alice->>John: Hello John, how are you?
John-->>Alice: Great!
Alice-)John: See you later!