正确使用错误 [英] Proper use of errors

查看:88
本文介绍了正确使用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将TypeScript用于一个相当大的项目,并且想知道使用 Error s的标准是什么。例如,假设我在Java中将索引超出范围:

I'm using TypeScript for a reasonably large project, and am wondering what the standard is for the use of Errors. For example, say I hand an index out of bounds exception in Java:

throw new IndexOutOfBoundsException();

TypeScript中的等效语句是否为:

Would the equivalent statement in TypeScript be:

throw new Error("Index Out of Bounds");

我还能通过哪些其他方式完成此任务?可接受的标准是什么?

What other ways could I accomplish this? What is the accepted standard?

推荐答案

有人在评论中将此链接发布到了MDN,我认为这非常有帮助


EvalError ---创建代表错误的实例,该错误有关全局函数eval()。

EvalError --- Creates an instance representing an error that occurs regarding the global function eval().

InternalError -创建一个实例,该实例表示当JavaScript引擎中的内部错误为抛出。例如。
太多的递归。

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 ---创建一个实例,该实例表示当数值变量或参数发生时发生的错误超出其有效的
范围。

RangeError --- Creates an instance representing an error that occurs when a numeric variable or parameter is outside of its valid range.

ReferenceError ---创建一个实例,该实例表示在取消引用对象时发生的错误无效引用。

ReferenceError --- Creates an instance representing an error that occurs when de-referencing an invalid reference.

SyntaxError -创建一个实例,该实例表示在eval()中解析代码时发生的语法错误。

SyntaxError --- Creates an instance representing a syntax error that occurs while parsing code in eval().

TypeError -创建一个实例,该实例表示当变量或参数不是有效类型时发生的错误。

TypeError --- Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.

URIError -创建一个实例,该实例表示在将encodeURI()或encodeURI()传递无效参数时发生的错误。

URIError --- Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.

这篇关于正确使用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆