typescript 正确使用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23790509/
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
Proper use of errors
提问by Nathan Bellowe
I'm using TypeScript for a reasonably large project, and am wondering what the standard is for the use of Error
s. For example, say I hand an index out of bounds exception in Java:
我正在将 TypeScript 用于一个相当大的项目,并且想知道使用Error
s的标准是什么。例如,假设我在 Java 中处理了一个索引越界异常:
throw new IndexOutOfBoundsException();
Would the equivalent statement in TypeScript be:
TypeScript 中的等效语句是:
throw new Error("Index Out of Bounds");
What other ways could I accomplish this? What is the accepted standard?
我还有什么其他方法可以做到这一点?什么是公认的标准?
回答by Nathan Bellowe
Someone posted this link to the MDN in a comment, and I think it was very helpful. It describes things like ErrorTypes very thoroughly.
有人在评论中将此链接发布到 MDN,我认为这很有帮助。它非常彻底地描述了诸如 ErrorType 之类的东西。
EvalError--- Creates an instance representing an error that occurs regarding the global function eval().
InternalError--- Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
RangeError--- Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.
ReferenceError--- Creates an instance representing an error that occurs when de-referencing an invalid reference.
SyntaxError--- Creates an instance representing a syntax error that occurs while parsing code in eval().
TypeError--- Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.
URIError--- Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.
EvalError--- 创建一个实例,表示在全局函数 eval() 中发生的错误。
InternalError--- 创建一个实例,表示在 JavaScript 引擎中引发内部错误时发生的错误。例如“太多的递归”。
RangeError--- 创建一个实例,表示当数值变量或参数超出其有效范围时发生的错误。
ReferenceError--- 创建一个实例,表示取消引用无效引用时发生的错误。
SyntaxError--- 创建一个实例,表示在解析 eval() 中的代码时发生的语法错误。
TypeError--- 创建一个实例,表示当变量或参数不是有效类型时发生的错误。
URIError--- 创建一个实例,表示当 encodeURI() 或 decodeURI() 传递无效参数时发生的错误。
回答by basarat
The convention for out of range in JavaScript is using RangeError
. To check the type use if / else + instanceof
starting at the most specific to the most generic
JavaScript 中超出范围的约定是使用RangeError
. 要检查类型使用 if / else +instanceof
从最具体到最通用
try {
throw new RangeError();
}
catch (e){
if(e instanceof RangeError){
console.log('out of range');
}
}
回答by Maciej Sikora
Simple solution to emit and show message by Exception.
通过异常发出和显示消息的简单解决方案。
try {
throw new TypeError("Error message");
}
catch (e){
console.log((<Error>e).message);//conversion to Error type
}
Caution
警告
Above is not a solution if we don't know what kind of error can be emitted from the block. In such cases type guards should be used and proper handling for proper error should be done - take a look on @Moriarty answer.
如果我们不知道块会发出什么样的错误,以上不是解决方案。在这种情况下,应该使用类型保护并正确处理正确的错误 - 看看@Moriarty 的回答。
回答by Moriarty
Don't forget about switch statements:
不要忘记 switch 语句:
- Ensure handling with
default
. instanceof
can match on superclass.- ES6
constructor
will match on the exact class. - Easier to read.
- 确保使用
default
. instanceof
可以匹配超类。- ES6
constructor
将匹配确切的类。 - 更容易阅读。
function handleError() {
try {
throw new RangeError();
}
catch (e) {
switch (e.constructor) {
case Error: return console.log('generic');
case RangeError: return console.log('range');
default: return console.log('unknown');
}
}
}
handleError();