private static String getBookByNumber(String[] books)
throws Exception {
while (true) {
System.out.println("输入图书序号:");
try {
//获取输入的图书序号(数组下标)
int index = inputCommand();
//若返回值为-1
if(index == -1){
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
//若不出现”数组下标越界异常“,则返回相应位置的图书
String book = books[index];
return book;
} catch (ArrayIndexOutOfBoundsException e) {
//输入的序号不存在(引发”数组下标越界异常“),则抛出”图书不存在异常“
Exception bookNotExists = new Exception("图书不存在!");
bookNotExists.initCause(e);
throw bookNotExists;
}
}
}
ziom
相关分类