Custom exception type

前端 未结 13 1745
生来不讨喜
生来不讨喜 2020-11-28 17:40

Can I define custom types for user-defined exceptions in JavaScript? If so, how would I do it?

13条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 18:15

    ES6

    With the new class and extend keywords it’s now much easier:

    class CustomError extends Error {
      constructor(message) {
        super(message);
        //something
      }
    }
    

提交回复
热议问题