mardi 18 décembre 2018

Validate values and throw an exception

I need to validate startIndex and endIndex, and throw an IndexOutOfBoundsException if they are invalid. I am trying to throw an exception on purpose. This code throws the exception, but stops the program. I need to handle the exception and have the program continue. I need some help with this code. Thanks in advance

public int findMinReadingIndex2(int startIndex, int endIndex) { if (startIndex < 0 || endIndex < 0 || endIndex >= this.sensorReadings.size()) throw new IndexOutOfBoundsException("Index out of bounds: 0 - " + (this.sensorReadings.size() - 1)); else sensorReadings.get(startIndex); int minIndex = 0; startIndex = -1; endIndex = 18; for (int i = startIndex; i < sensorReadings.size(); i++) { if (i >= startIndex && i <= endIndex) { if (this.sensorReadings.get(minIndex).getValue() < this.sensorReadings.get(i).getValue()) minIndex = i; } } return minIndex; }

Aucun commentaire:

Enregistrer un commentaire