Javascript 抛出字符串而不是错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11502052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Throwing strings instead of Errors
提问by billc.cn
Since we can throw anything with the throw
keyword in Javascript, can't we just throw an error message string directly?
既然我们可以throw
在Javascript中用关键字抛出任何东西,那么我们不能直接抛出一个错误消息字符串吗?
Does anyone know any catch in this?
有谁知道这有什么问题吗?
Let me add some background to this: Very often, in the JavaScript world, people rely on parameter checking as opposed to using the try-catch mechanism, so it makes sense to only throw fatal errors with throw
. Still, to be able to catch some system Errors, I have to use a different class for my own errors and instead of creating a subclass of Error, I think I should just use String.
让我为此添加一些背景知识:在 JavaScript 世界中,人们通常依赖参数检查而不是使用 try-catch 机制,因此仅使用throw
. 尽管如此,为了能够捕获一些系统错误,我必须为自己的错误使用不同的类,而不是创建 Error 的子类,我认为我应该只使用 String。
采纳答案by lanzz
It is okay to throw whatever you like, but keep in mind that if the catch is outside of your own code, it might expect a full Error instance and not a plain string.
可以抛出任何你喜欢的东西,但请记住,如果 catch 不在你自己的代码之外,它可能需要一个完整的 Error 实例而不是一个普通的字符串。
回答by Bergi
Yes, you can throw other values, but it's not a good practice.
Does anyone know any catch in this?
有谁知道这有什么问题吗?
A string is not an error object, and does not convey any useful debugging information. Devtools rely on that, such as the file and line where the error was created, the stacktrace at the throw
location etc, which are available as properties on Error
objects.
字符串不是错误对象,并且不传达任何有用的调试信息。Devtools 依赖于它,例如创建错误的文件和行、该throw
位置的堆栈跟踪等,这些都可用作Error
对象的属性。
Whenever you think of throwing a primitive string value, throw a new Error("<the string>")
instead.
每当您想到抛出原始字符串值时,请new Error("<the string>")
改为抛出 a 。
回答by MaxArt
You can throw errors with messages, you know.
你可以用消息抛出错误,你知道的。
try {
throw new Error("This is an error");
} catch (e) {
alert(e.message); // This is an error
}
But you canactually throw strings:
但你实际上可以抛出字符串:
try {
throw "This is an error";
} catch (e) {
alert(e); // This is an error
}
回答by Timothy C. Quinn
As others have mentioned above, if you are not throwing an Error object, then you must have try/catch blocks to trap these objects and handle them appropriately or else be in a world of hurt for debugging.
正如其他人在上面提到的,如果你没有抛出一个 Error 对象,那么你必须有 try/catch 块来捕获这些对象并适当地处理它们,否则调试就会受到伤害。
However, when it comes to throwing Errors for non-error handling purposes like controlling program flow, this may be a helpful way to utilize throw
without an Error.
然而,当为了控制程序流等非错误处理目的而抛出错误时,这可能是一种throw
没有错误的有用方法。
When using throws to control program flow, it can be inefficient in any language as the runtime will often do a lot of heavy lifting to unwind call stack information and serialize the data so its available to the user land scope. By avoiding Error creation, you can avoid this performance hit. The key is that you must have a handler up the call stack that knows how to handle this situation. For instance if you throw {isHardStop: true, stopCode: SOME_CODE}
and design the handlers to detect this, you may be able to flatten out some of your code or choose cleaner syntax.
当使用 throws 来控制程序流时,它在任何语言中都可能是低效的,因为运行时通常会做很多繁重的工作来展开调用堆栈信息并序列化数据,使其可用于用户范围。通过避免错误创建,您可以避免这种性能下降。关键是您必须在调用堆栈上有一个知道如何处理这种情况的处理程序。例如,如果您throw {isHardStop: true, stopCode: SOME_CODE}
设计处理程序来检测这一点,您可能能够扁平化一些代码或选择更简洁的语法。
Your handler for this ladder case could be structured like:
这个梯子案例的处理程序可以是这样结构的:
try { ... } catch(thr) {
if(!thr){
// Is not Error or Json - Handle accordingly
} else if(thr.isHardStop){
// Handle the stop
} else {
// Most likely a real error. Handle accordingly
}
}
回答by Willem van der Veen
Although you can throw any type of data that you'd like, this is not optimal when debugging. A JS Error
object contains all kind of information regarding the Error and a message. Whereas a string can only contain a message.
尽管您可以抛出您想要的任何类型的数据,但这在调试时并不是最佳选择。JSError
对象包含有关 Error 和消息的所有类型的信息。而字符串只能包含一条消息。
This additional information includes:
这些附加信息包括:
- fileName: from which file the error was thrown
- Linenumber: from which line the error was thrown
- stacktrace: from which function the error was called
- fileName:从哪个文件抛出错误
- Linenumber:从哪一行抛出错误
- 堆栈跟踪:从哪个函数调用错误
Here is for example a stacktrace from chrome devtools:
例如,这是来自 chrome devtools 的堆栈跟踪: