我想编写一个通用的错误处理程序,它将捕获在代码的任何实例中故意抛出的自定义错误。

当我抛出新的错误('sample'),如下所示

try {
    throw new Error({'hehe':'haha'});
    // throw new Error('hehe');
} catch(e) {
    alert(e);
    console.log(e);
}

日志在Firefox中显示为错误:[object object],我无法解析该对象。

对于第二次抛出,日志显示为:错误:呵呵

然而当我这样做的时候

try {
    throw ({'hehe':'haha'});
} catch(e) {
    alert(e);
    console.log(e);
}

控制台显示为:对象{hehe="haha"},我可以在其中访问错误属性。

有什么不同?

区别是否如代码中所示?比如字符串会作为字符串传递而对象作为对象但语法会有所不同?

我还没有探索过抛出错误对象…我只抛出了字符串。

除了以上两种方法,还有别的办法吗?


当前回答

这是相当古老的,但希望任何搜索它的人仍然可以从中学到东西:

首先,在javascript中,我们有一个叫做原始包装器的东西;原语包装器接受原语数据,并通过简单地使用“构造函数模式”以对象格式表示它。不过,在原语包装器中,您可以决定将数据作为对象类型返回,也可以将其作为原语类型返回(在这种情况下,您现在得到了一个让javascript提取原语值的go-ahead命令,在这种情况下,您不使用new关键字)。

总而言之:

throw "My error": this creates an Error object and returns the primitive data extracted from the constructor "this" object. And if you try checking the typeof in the catch block, it tells you its a primitive typeof "string" throw new Error("My error"): this returns you an object where you can access the error value from the message property. What simply happens here is that the "new keyword" constructs a "this" object and assign "{name:"Error",message:"..."}" to it and returns it. And when you try to check the typeof from the catch block, you will see a typeof "object".

注意:在显式地将自定义对象传递给throw的情况下,它的行为就像使用new关键字调用构造函数一样,因此,catch块将返回自定义对象而不是消息属性值。例如:throw {name:"RangeError",message:"range is out of scope",environment:" occurred in testing function"}。

总之,使用任何适合你的东西,你知道你在做什么。但对于我来说,如果我不需要太多的数据,而只是需要错误,那么我就使用原始返回器。

其他回答

你可以把它作为对象抛出

throw ({message: 'This Failed'})

例如在try/catch中

try {
//
} catch(e) {
    console.log(e); //{message: 'This Failed'}
    console.log(e.message); //This Failed
}

或者只是抛出一个字符串错误

throw ('Your error')

try {
//
} catch(e) {
    console.log(e); //Your error
}

throw new Error //only accept a string

Throw something对对象和字符串都有效。但是它比另一种方法更不受支持。throw new Error("")将只对字符串起作用,并在catch块中将对象转化为无用的[Object obj]。

'throw new Error'和'throw someObject'在javascript中的区别在于,throw new Error将传递给它的错误以以下格式包装

{name: '错误',消息:'传入构造函数的字符串' }

throw someObject将按原样抛出对象,并且不允许从try块执行任何进一步的代码,即与throw new Error相同。

下面是关于Error对象和抛出自己的错误的一个很好的解释

错误对象

在发生错误时,我们能从中提取什么?所有浏览器中的Error对象都支持以下两个属性:

name:错误的名称,或者更具体地说,错误所属的构造函数的名称。 message:错误的描述,该描述因浏览器而异。

name属性可以返回六个可能的值,如前所述,它对应于错误构造函数的名称。它们是:

Error Name          Description

EvalError           An error in the eval() function has occurred.

RangeError          Out of range number value has occurred.

ReferenceError      An illegal reference has occurred.

SyntaxError         A syntax error within code inside the eval() function has occurred.
                    All other syntax errors are not caught by try/catch/finally, and will
                    trigger the default browser error message associated with the error. 
                    To catch actual syntax errors, you may use the onerror event.

TypeError           An error in the expected variable type has occurred.

URIError            An error when encoding or decoding the URI has occurred 
                   (ie: when calling encodeURI()).

抛出自己的错误(异常)

在控制自动从try块转移到catch块之前,您不必等待6种类型错误中的一种发生,还可以显式地抛出自己的异常,以强制按需发生这种情况。这对于创建关于错误是什么以及何时应该将控制转移到catch的定义非常有用。

Error构造函数用于创建错误对象。发生运行时错误时抛出错误对象。Error对象还可以用作用户定义异常的基对象。

用户定义错误通过throw语句抛出。程序控制将被传递给调用堆栈中的第一个catch块。

有和没有error对象时抛出错误的区别:


throw {'hehe':'haha'};

在chrome中,devtools是这样的:

Chrome告诉我们,我们有一个未捕获的错误,这只是一个JS对象。对象本身可能有关于错误的信息,但我们仍然不知道它来自哪里。在我们处理代码和调试它的时候,这不是很有用。


throw new Error({'hehe':'haha'}); 

在chrome中,devtools是这样的:

与error对象一起抛出的错误在展开堆栈时提供堆栈跟踪。这为我们提供了有价值的信息,错误准确地来自哪里,这在调试代码时通常是有价值的信息。进一步注意,错误说的是[object object],这是因为error构造函数期望一个消息字符串作为第一个参数。当它接收到一个对象时,它会将其强制转换为一个字符串。

这是相当古老的,但希望任何搜索它的人仍然可以从中学到东西:

首先,在javascript中,我们有一个叫做原始包装器的东西;原语包装器接受原语数据,并通过简单地使用“构造函数模式”以对象格式表示它。不过,在原语包装器中,您可以决定将数据作为对象类型返回,也可以将其作为原语类型返回(在这种情况下,您现在得到了一个让javascript提取原语值的go-ahead命令,在这种情况下,您不使用new关键字)。

总而言之:

throw "My error": this creates an Error object and returns the primitive data extracted from the constructor "this" object. And if you try checking the typeof in the catch block, it tells you its a primitive typeof "string" throw new Error("My error"): this returns you an object where you can access the error value from the message property. What simply happens here is that the "new keyword" constructs a "this" object and assign "{name:"Error",message:"..."}" to it and returns it. And when you try to check the typeof from the catch block, you will see a typeof "object".

注意:在显式地将自定义对象传递给throw的情况下,它的行为就像使用new关键字调用构造函数一样,因此,catch块将返回自定义对象而不是消息属性值。例如:throw {name:"RangeError",message:"range is out of scope",environment:" occurred in testing function"}。

总之,使用任何适合你的东西,你知道你在做什么。但对于我来说,如果我不需要太多的数据,而只是需要错误,那么我就使用原始返回器。