相关疑难解决方法(0)

java中的Arrays.copyOfRange方法抛出不正确的异常

我今天在阵列上工作,突然间我遇到了一个抛出意外异常的场景.

如果你看下面的代码,我认为它必须抛出ArrayIndexOutOfBoundsException,但令人惊讶的是它IllegalArgumentException反而投掷:

import java.util.Arrays;
public class RangeTest {
public static void main(String[] args) {
    int[] a = new int[] {0,1,2,3,4,5,6,7,8,9};
    int[] b = Arrays.copyOfRange(a, Integer.MIN_VALUE, 10);
    // If we'll use Integer.MIN_VALUE+100 instead Integer.MIN_VALUE,
    // OutOfMemoryError will be thrown
    for (int k = 0; k < b.length; k++)
        System.out.print(b[k] + " ");
   }
}
Run Code Online (Sandbox Code Playgroud)

如果我弄错了,有人可以帮助我,让我知道吗?

java arrays exception-handling exception try-catch

18
推荐指数
1
解决办法
2749
查看次数

标签 统计

arrays ×1

exception ×1

exception-handling ×1

java ×1

try-catch ×1