private static String getBookByNumber(String[] books)???throws Exception {??while (true) {???System.out.println("輸入圖書序號(hào):");???try {????//獲取輸入的圖書序號(hào)(數(shù)組下標(biāo))????int index = inputCommand();????//若返回值為-1????if(index == -1){?????System.out.println("命令輸入錯(cuò)誤!請根據(jù)提示輸入數(shù)字命令!");?????continue;????}????//若不出現(xiàn)”數(shù)組下標(biāo)越界異常“,則返回相應(yīng)位置的圖書????String book = books[index];????return book;???} catch (ArrayIndexOutOfBoundsException e) {????//輸入的序號(hào)不存在(引發(fā)”數(shù)組下標(biāo)越界異?!埃?,則拋出”圖書不存在異?!????Exception bookNotExists = new Exception("圖書不存在!");????bookNotExists.initCause(e);????throw bookNotExists;???}??}?}
添加回答
舉報(bào)
0/150
提交
取消