I'm making a custom error by inheriting Error and have some confusion regarding its different behavior from Error.
var util = require('util');
var BusinessLogicError = function (message) {
Error.captureStackTrace(this, this);
this.message = message || 'Error';
};
util.inherits(BusinessLogicError, Error);
BusinessLogicError.prototype.name = 'BusinessLogic Error';
function throwError() {
throw Error("Error occurred.");
}
function throwCustomError() {
throw BusinessLogicError("Custom Error occurred.");
}
If I throw BusinessLogicError without new keyword , I get BusinessLogicError as undefined. While Error works just fine.
throwError()
results in :
throw Error("Error occurred."); ^ Error: hello at Error ()
throwCustomError()
results in :
throw BusinessLogicError("hello "); ^ undefined
I would like to know why BusinessLogicError fails without new ?