var Controller = function () {
try {
throw new this.userException('test', 'test');
} catch (error) {
if (error instanceof this.userException) {
console.error(error.stack);
}
}
}
Controller.prototype.userException = function (name, message, htmlMessage) {
this.name = name;
this.message = message;
this.htmlMessage = htmlMessage;
Error.captureStackTrace(this, this.userException);
this.toString = function () {
return name + ' : ' + message;
}
};
While I was trying to create custom exceptions i found this Error.captureStackTrace
in Node JS document. First i used it like below.
Error.captureStackTrace(this, userException);
Which console.error(error)
gave no output.By mistake I changed the line to
Error.captureStackTrace(this, this.userException);
Which return object the contains properties of userException
methods. Also console.error(error.stack);
prints the stack trace
. I'm so confused how does this
keyword works in this statement Error.captureStackTrace(this, this.userException);
. what are this
keywords of first and second arguments pointed to? is it to the same Controller
object? But again, why i had to provide this
as the first and second argument, in order this to work?