抛出不了异常。
求助:我这个程序的按照名称查找抛出不了异常。
package com.imooc.exception_test; import java.util.Scanner; public class Book { public static void main(String[] args) { // 用字符串数组保存书籍信息 String[] book = { "数据结构", "Java实战开发", "html5", "javascript", "高等数学" }; Book myBook = new Book(); // 处理IO System.out.println("按照命令查找书籍:1-按照名称查找书籍;2-按照序号查找书籍"); Scanner scanner1 = new Scanner(System.in); try { int k = scanner1.nextInt(); if (k == 1) { System.out.println("请输入书籍的名字:"); Scanner scanner2 = new Scanner(System.in); String c = scanner2.nextLine(); myBook.findByname(c, book); } if (k == 2) { System.out.println("请输入书籍的序号"); Scanner scanner3 = new Scanner(System.in); int c = scanner3.nextInt(); myBook.findByIndex(c, book); } } catch (Exception e) { // TODO: handle exception System.out.println("命令输入错误,请按照提示输入数字命令"); } } // 按名查找,抛出“书名不存在异常” public void findByname(String input, String book[]) { try { for (int i = 0; i < book.length; i++) { if (book[i] == input) { System.out.println("book:" + book[i].toString()); } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); System.out.println("书名不存在异常"); } } // 按序号查找,抛出“序号不存在异常” public void findByIndex(int index, String book[]) { try { System.out.println("book:" + book[index].toString()); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); System.out.println("这本书的序号不存在异常!"); } } }