为什么我的这个代码没有捕捉到异常?
Main:
package com.imooc; public class Main { public static void main(String[] args){ Book book = new Book(); book.input(); } }
BookInputError:
package com.imooc; class BookInputError extends Exception { BookInputError() { super(); } }
Book:
package com.imooc; import java.util.Scanner; public class Book { public void input () { String name; String number; Scanner scanner = new Scanner(System.in); name = scanner.nextLine(); number = scanner.nextLine(); try { for (int i = 0; i <= number.length()-1; i++) { if(number.charAt(i) < '0' && number.charAt(i) > '9') { throw new BookInputError(); } } } catch (BookInputError bookInputError) { System.out.println("input error"); } System.out.println(name+number); } }
输入实例:
8989 uuu
这个我觉得应该抛出异常,结果没有。