简体   繁体   English

如何在游戏中处理ArrayIndexOutOfBoundsException

[英]How to handle ArrayIndexOutOfBoundsException in a game

What would be a good way to handle ArrayIndexOutOfBoundsException for this case. 在这种情况下,处理ArrayIndexOutOfBoundsException的好方法是什么。 Or ignore it? 还是忽略它?

public Attribute getAttribute (int index) {
    return attributes.get(index);
}

A bug like this should not happen. 这样的错误不应该发生。 The amount of Attributes are fixed in the project. 属性的数量在项目中是固定的。 Meaning I always know how many there are. 意思是我总是知道有多少个。 I have a HashMap<String, String> with their name and description 我有一个HashMap<String, String>及其名称和描述

Somewhat follow up from: 来自:

Throw Exception or prevent it? 抛出异常还是阻止异常?

What would be an acceptable error handling of this situation? 在这种情况下可以接受的错误处理是什么? Or is it completely unnecessary? 还是完全没有必要?

If this should not happen as per the invariants and preconditions of the method, there is no real need to handle the exception. 如果按照方法的不变式和前提条件不应该发生这种情况,则无需真正处理该异常。 If you want to check to be make sure no bugs crop up during development, typically you would use assert for something like this: 如果要检查以确保在开发过程中不会出现任何错误,通常可以使用assert来实现以下目的:

public Attribute getAttribute (int index) {
    assert(index >= 0 && index < attributes.size());
    return attributes.get(index);
}

Use assert to make sure that preconditions and invariants are not violated. 使用assert来确保不违反前提条件和不变式。 assert shouldn't be used for checking of "normal" error conditions, but it is appropriate for debug / development sanity checks. assert不应用于检查“正常”错误情况,但适用于调试/开发健全性检查。 It self-documents your assumptions. 它会自动记录您的假设。 Also, it will allow a bug to be caught here , rather than at some vague higher level where an exception is eventually caught. 而且,它允许在这里捕获错误,而不是在某个模糊的更高级别上捕获最终的异常。 This can help you pinpoint problems quicker. 这可以帮助您更快地查明问题。

For more information, some explanation and examples, check out programming with assertions . 有关更多信息,一些解释和示例,请查看带有断言的编程

There is also a related question at Avoiding != null statements - related in that the answer discusses overly aggressive checking of assumptions, and how to deal with them effectively; 避免!=空语句也有一个相关的问题-与此相关的是,该答案讨论了对假设的过度积极检查,以及如何有效地处理它们。 a good read. 一本好书。

However : It's important to note that your method is public . 但是 :必须注意,您的方法是public The implication here is that anybody can call this. 这里的含义是任何人都可以称呼它。 Perhaps you will even use this code in another program in the future. 也许将来您甚至会在另一个程序中使用此代码。 You will want to consider that when deciding whether or not you want to throw the AIOOB. 在决定是否要抛出AIOOB时,您将需要考虑这一点。 If you do, I would just throw it out of the method, and document the method as throwing it if index is out of range. 如果这样做,我将把它扔出方法之外,并在index超出范围时将方法记录为扔掉它。 It depends on your situation, really. 确实取决于您的情况。

Also as Christian Hujer mentions in the comments below, a good set of test suites will help make sure your code never surprises you with any bugs here in the first place. 同样,正如Christian Hujer在下面的评论中提到的那样,一组良好的测试套件将帮助确保您的代码从一开始就不会因任何错误而使您感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM