简体   繁体   English

如果数组中不存在元素,如何引发数据异常?

[英]How to throw a data exception if an element does not exist in Array?

So i have an arraylist of strings, im currently using a method so the user can access and return certain elements from the arraylist by inputting an index number. 所以我有一个字符串数组列表,我目前正在使用一种方法,这样用户可以通过输入索引号来访问和返回数组列表中的某些元素。

I want to throw my own Data Exception if they try to access the index number of something which is not in the array. 如果他们尝试访问不在数组中的内容的索引号,我想抛出自己的数据异常。 At the minute it is throwing an Index out of bounds exception. 在一分钟,它抛出了索引超出范围的异常。 Currently im using the if statement below however it is not working! 目前,我正在使用下面的if语句,但是它不起作用! How can i do this? 我怎样才能做到这一点?

if (set.get(index) == null) {
        throw new DataException("Record does not exists!");
    }

Attempting to access an out-of-bounds index from an ArrayList will always throw an IndexOutOfBounds exception. 尝试从ArrayList访问越界索引将始终抛出IndexOutOfBounds异常。 To fix this, you have two options. 要解决此问题,您有两个选择。 You can either avoid asking for the element until you're sure it exists or you can catch the error. 您可以在确定元素存在之前避免要求该元素,否则可以捕获错误。

To catch the error, you would use a try-catch block like so: 要捕获该错误,您可以使用try-catch块,如下所示:

try {
    someVariable = set.get(index);
} catch(Exception e) {
    throw new DataException(...);
}

To avoid triggering the error in the first place, you can just make sure the index is within the bounds of the ArrayList like so: 为了避免首先触发错误,您可以确保索引位于ArrayList的范围内,如下所示:

if(index < 0 || index >= set.size()) {
    throw new DataException(...);
}

You just need to test that the index is neither negative nor superior or equal to the list size: 您只需要测试索引既不是负数也不是大于或等于列表大小:

if ( index < 0  || index >= list.size() ) {
    throw new DataException("Record does not exists!");
}

As the List.get(int index) javadoc stays : 由于List.get(int index) javadoc保持不变:

Throws: 抛出:

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()) IndexOutOfBoundsException-如果索引超出范围(索引<0 ||索引> = size())

Now I am not sure that it is good idea to wrap IndexOutOfBoundsException in a DataException . 现在,我不确定将IndexOutOfBoundsException包装在DataException是个好主意。
It is a programming error to access to an index of of bounds of the array. 访问数组的边界索引是编程错误。
IndexOutOfBoundsException conveys very well this idea while IndexOutOfBoundsException很好地传达了这个想法,而

throw new DataException("Record does not exists!"); may be understood as a client error. 可以理解为客户端错误。

change the condition to: 将条件更改为:

if(index < 0 || index >= set.size()){
     ... 
}
if (index < 0 || index >= set.size() || set.get(index) == null) {
    throw new DataException("Record does not exists!");
}

Test that the index is within the ArrayList before executing the get () to avoid the IndexOutOfBoundsException. 在执行get()之前,请测试索引是否在ArrayList中,以避免IndexOutOfBoundsException。 If either of the first 2 conditions are true, the get() is not reached. 如果前两个条件中的任何一个为true,则不会达到get()。

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

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