Custom exception type

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

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

13条回答
  •  -上瘾入骨i
    2020-11-28 17:58

    An alternative to the answer of asselin for use with ES2015 classes

    class InvalidArgumentException extends Error {
        constructor(message) {
            super();
            Error.captureStackTrace(this, this.constructor);
            this.name = "InvalidArgumentException";
            this.message = message;
        }
    }
    

提交回复
热议问题