首页 > 解决方案 > java捕获indexoutofbounds异常

问题描述

我用谷歌搜索了很多,但我不知道为什么我无法捕捉到indexoutofboundsexception错误。有人可以告诉我,我的错误是什么?

try
{
    if (amountOfPlayers <= 15 && amountOfPlayers > 0) {
        int i = 0;
        do {
            i++;
            System.out.println("Please enter your name: ");
            String name = scanner.next();
            nameList.add(name);
        } while (i < amountOfPlayers);
    }
}
catch (IndexOutOfBoundsException ex)
{
    System.out.println("Wrong input. Theres a maximum of 15 players allowed");
}

标签: try-catchindexoutofboundsexception

解决方案


推荐阅读