0

I'm trying to create a custom exception with some useful debug information:

var customError = function(name, message) {

    this.message = message;
    this.name = name;

    this.prototype = new Error(); // to make customError "inherit" from the 
                                  // default error
};

throw new customError('CustomException', 'Something went wrong'); 

But all I'm getting are fuzzy messages in the console:

IE: "SCRIPT5022: Exception thrown and not caught "

Firefox: "uncaught exception: [object Object]"

What do I need to change in order to get something useful out of my exceptions?

4

1 回答 1

1

Wrong usage of prototype:

var customError = function(name, message) {    
    this.message = message;
    this.name = name;
};

customError.prototype = new Error(); // to make customError "inherit" from the 
                                     // default error

throw new customError('CustomException', 'Something went wrong'); 

prototype is a property of the constructor (customError), not of the constructed object (this), see ECMA sec 4.2.1 and 4.3.5.

于 2013-03-30T22:07:53.730 回答