0

What would be a good way to handle ArrayIndexOutOfBoundsException for this case. 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

Somewhat follow up from:

Throw Exception or prevent it?

What would be an acceptable error handling of this situation? Or is it completely unnecessary?

Community
  • 1
  • 1
Emz
  • 1,280
  • 1
  • 14
  • 29
  • 1
    Perhaps this question could help. http://stackoverflow.com/questions/27157206/throw-exception-or-prevent-it/27157347#27157347 It is extremely similar. – Bryan Davis Nov 26 '14 at 19:45
  • @Emz You could default to something (default behavior) to make sure the game doesn't crash then send an error report or log it somewhere so you can later go back and find the bug... – brso05 Nov 26 '14 at 19:49
  • That question was by me. This is a somewhat follow up. As I wrote in my question. – Emz Nov 26 '14 at 19:50

1 Answers1

1

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:

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 shouldn't be used for checking of "normal" error conditions, but it is appropriate for debug / development sanity checks. 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. 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. 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. 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.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 2
    While this code is good, assertions also have their problems. "Defensive Programming leads to Offensive Code. The best defense is a good offense. And a good offense is a good suite of tests." - Robert C. Martin. I'm not saying that therefore the assertion is bad, I'm just saying that testing should also be considered. – Christian Hujer Nov 26 '14 at 19:48