What to return to IndexOutOfBoundsException?

I have the following method

private ArrayList<User> allUsers = new ArrayList<User>(); public User getUser(int index) { try { return allUsers.get(index); } catch(IndexOutOfBoundsException e) { // What should I return here?? Say that you want index 0 and no User // exists in the ArrayList allUsers, what should I then return? The // method needs a User to be returned } } 

And I'm not sure how to do this, I'm sure this is a simple fix, but what should I return to the catch block? Eclipse complains that it is necessary to return User .

+4
source share
8 answers

I would throw an exception if the user is not found. This exception is thrown when the method is called. You can change the code here to use a special exception if you want. Sort of:

 public User getUser(int index) throws IndexOutOfBoundsException { if(index >= allUsers.size()){ throw new IndexOutOfBoundsException("User doesn't exist"); } return allUsers.get(index); } 
+3
source

My common opinion is that you will never catch an exception that you do not know how to handle . In particular, in this case, since IndexOutOfBoundsException is a RuntimeException and therefore does not need to be caught, you can just let it distribute the call stack. The caller requests the object by the list index, so it seems to have some idea of ​​which index to request - then throwing or allowing the throwing of the thrown IndexOutOfBoundsException seems completely natural.

The only other obvious option would be to catch the exception and return null , but I really don't like this approach for such egregious errors on the part of the caller when there is no reasonable return value. You can also return a special instance of User (see the Zero Object Template), but even this does not relieve the caller of the responsibility for checking what has been returned. Depending on the interface and implementation of User such checks may be trivial or not, but they still need to be done.

If you want to clearly indicate that a method can throw an exception, just say this:

 public User getUser(int index) throws IndexOutOfBoundsException { ... } 

Or, like @ Bela Vizer , suppose you wrap it in an IllegalArgumentException (which is also a RuntimeException).

And as pointed out by @ lc. it’s best to first check if the object exists before trying to access it. Handle the error you are expecting rather than relying on the get() method to throw an exception. You should still be clearly aware that the method may cause such an exception, however, if, for example, the collection is modified between validation and return. With multi-threaded software on multi-core processors, as you know, unfamiliar things happen.

+15
source

Ask yourself the question: "what should you return if you want index 0 and no user?" and return everything that you answer.

If you do not have an answer, you must either throw the exception or not catch it first.

Note that often the response time will return null if the valid behavior is to request a nonexistent user.


Side comment: It is usually considered “good practice” not to rely on the exception of fishing, but first check the error conditions. In your case, you first try to get to specify an invalid object index, and then react if the getter explodes. Instead, I would recommend testing the index parameter first (make sure that it is at least zero and less than the length of allUsers ), and if it fails the test to do something (return null or throw your own exception).

+11
source

There are many possibilities. Some of them depend on your beliefs.

  • You can raise an IllegalArgumentException because the argument passed is not valid.

  • You can throw an IndexOutOfBoundsException exception.

  • If you want the clients (the callers of this method) to accept, you can even declare an exception (specify an exceptional class that extends the exception), because IllegalArgumentException and IndexOutOfBoundsException are the exception runtime, which means you don't need to explicitly prepare.

I usually check if the index is in the range if it doesn't return null, and in javadoc it is mentioned that it can return null if ...

+4
source

Do not listen to Eclipse.

You have two options, both of which can be good or bad, depending on the circumstances.

  • You can return null .
  • You can simply throw the exception (or rather, not even catch it) by asking the invocation method to handle it.

There are several more options, but the main choice between the two above is to handle the problem or delegate the task to the caller.

From this code it is impossible to determine which one is the right solution, only you can know what is more appropriate for the situation.

Whichever choice you choose, it is better to check the index manually ( 0<=index<allUsers.size() ) and not rely on RuntimeException for normal program behavior.

+2
source

Assuming index is a user, just let IndexOutOfBoundsException be IndexOutOfBoundsException and catch it further at the point where you can display the error message.

In fact, you can first check index on allUsers.size() before trying to search for the same. User input should usually be verified as soon as possible.

+2
source

I would return null , however, if you feel that your application may suffer because of this, you can simply return "nobody" to the user, like this:

 return new User("nobody", ...); 

And handle this case outside the function.

Another alternative is to throw an exception and handle it outside.

+2
source

You have several options:

  • Return a null . Do this if the user can reasonably enter any index
  • Do not catch the exception. Do this if it is completely unexpected that the user can enter bad data, in other words, the input is already checked, so this is a “programming error” (or error) that caused the situation.
  • Throw checked exception. Do this if you feel that the calling code can and should deal with the problem.
+1
source

Source: https://habr.com/ru/post/1446680/


All Articles