如何在JavaScript中创建自定义错误? [英] How do I create a custom Error in JavaScript?
问题描述
由于某种原因,构造函数委派在以下代码段中不起作用:
函数NotImplementedError(){
Error.apply(this,arguments);
}
NotImplementedError.prototype = new Error();
var nie = new NotImplementedError(some message);
console.log(消息是:+ nie.message +')
运行它给出消息是:''
。任何想法为什么,或者如果有更好的方法来创建一个新的错误
子类? c $ c> c c c
更新您的代码,将您的原型分配给Error.prototype,instanceof和您的断言工作。
函数NotImplementedError(message){
this.name =NotImplementedError;
this.message =(message ||);
}
NotImplementedError.prototype = Error.prototype;
但是,我只是抛出自己的对象,只需检查name属性。
throw {name:NotImplementedError,message:too lazy to implement};
根据评论进行修改
查看评论后,试图记住为什么我会将原型分配给 Error.prototype
而不是 new Error()
像Nicholas Zakas在他的文章,我创建了一个 jsFiddle ,其代码如下: p>
function NotImplementedError(message){this.name =NotImplementedError; this.message =(message ||);} NotImplementedError.prototype = Error.prototype; function NotImplementedError2(message){this.message =(message ||);} NotImplementedError2.prototype = new Error(); try {var e = new NotImplementedError(NotImplementedError message); throw e;} catch(ex1){console.log(ex1.stack); console.log(ex1 instanceof NotImplementedError =+(ex1 instanceof NotImplementedError)); console.log(ex1 instanceof Error =+(ex1 instanceof Error)); console.log(ex1.name =+ ex1.name); console.log(ex1.message =+ ex1.message);} try {var e = new NotImplementedError2(NotImplementedError2 message); throw e;} catch(ex1){console.log(ex1.stack); console.log(ex1 instanceof NotImplementedError2 =+(ex1 instanceof NotImplementedError2)); console.log(ex1 instanceof Error =+(ex1 instanceof Error)); console.log(ex1.name =+ ex1.name); console.log(ex1.message =+ ex1.message);}
控制台输出是这样的。
undefined
ex1 instanceof NotImplementedError = true
ex1 instanceof错误= true
ex1.name = NotImplementedError
ex1.message = NotImplementedError消息
错误
在window.onload(http://fiddle.jshell.net/MwMEJ/show/ :29:34)
ex1 instanceof NotImplementedError2 = true
ex1 instanceof Error = true
ex1.name =错误
ex1.message = NotImplementedError2消息
这证实了我遇到的问题是错误的堆栈属性是行号,其中新错误()
被创建,而不是发生 throw e
的位置。但是,对于影响Error对象的 NotImplementedError.prototype.name =NotImplementedError
行的副作用可能会更好。
另外,当我没有明确地设置 .name
时,请注意 NotImplementedError2
等于错误。但是,如注释所述,由于该版本将原型设置为 new Error()
,我可以设置 NotImplementedError2.prototype.name =NotImplementedError2
并且OK。
For some reason it looks like constructor delegation doesn't work in the following snippet:
function NotImplementedError() {
Error.apply(this, arguments);
}
NotImplementedError.prototype = new Error();
var nie = new NotImplementedError("some message");
console.log("The message is: '"+nie.message+"'")
Running this gives The message is: ''
. Any ideas as to why, or if there is a better way to create a new Error
subclass? Is there a problem with apply
ing to the native Error
constructor that I don't know about?
Update your code to assign your prototype to the Error.prototype and the instanceof and your asserts work.
function NotImplementedError(message) {
this.name = "NotImplementedError";
this.message = (message || "");
}
NotImplementedError.prototype = Error.prototype;
However, I would just throw your own object and just check the name property.
throw {name : "NotImplementedError", message : "too lazy to implement"};
Edit based on comments
After looking at the comments and trying to remember why I would assign prototype to Error.prototype
instead of new Error()
like Nicholas Zakas did in his article, I created a jsFiddle with the code below:
function NotImplementedError(message) {
this.name = "NotImplementedError";
this.message = (message || "");
}
NotImplementedError.prototype = Error.prototype;
function NotImplementedError2(message) {
this.message = (message || "");
}
NotImplementedError2.prototype = new Error();
try {
var e = new NotImplementedError("NotImplementedError message");
throw e;
} catch (ex1) {
console.log(ex1.stack);
console.log("ex1 instanceof NotImplementedError = " + (ex1 instanceof NotImplementedError));
console.log("ex1 instanceof Error = " + (ex1 instanceof Error));
console.log("ex1.name = " + ex1.name);
console.log("ex1.message = " + ex1.message);
}
try {
var e = new NotImplementedError2("NotImplementedError2 message");
throw e;
} catch (ex1) {
console.log(ex1.stack);
console.log("ex1 instanceof NotImplementedError2 = " + (ex1 instanceof NotImplementedError2));
console.log("ex1 instanceof Error = " + (ex1 instanceof Error));
console.log("ex1.name = " + ex1.name);
console.log("ex1.message = " + ex1.message);
}
The console output was this.
undefined
ex1 instanceof NotImplementedError = true
ex1 instanceof Error = true
ex1.name = NotImplementedError
ex1.message = NotImplementedError message
Error
at window.onload (http://fiddle.jshell.net/MwMEJ/show/:29:34)
ex1 instanceof NotImplementedError2 = true
ex1 instanceof Error = true
ex1.name = Error
ex1.message = NotImplementedError2 message
This confirmes the "problem" I ran into was the stack property of the error was the line number where new Error()
was created, and not where the throw e
occurred. However, that may be better that having the side effect of a NotImplementedError.prototype.name = "NotImplementedError"
line affecting the Error object.
Also, notice with NotImplementedError2
, when I don't set the .name
explicitly, it is equal to "Error". However, as mentioned in the comments, because that version sets prototype to new Error()
, I could set NotImplementedError2.prototype.name = "NotImplementedError2"
and be OK.
这篇关于如何在JavaScript中创建自定义错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!