//产生原因:下标超出现有数组的长度范围int[] nums ={1,3,5};// 产生数组下标越界异常
System.out.println(nums[-1]);//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
NumberFormatException数字格式化异常
//产生原因:将非数字内容的字符串转换为数字时产生
String strNum =" 2e5d98 ";int number = Integer.parseInt(strNum);
System.out.println(number *100);//Exception in thread "main" java.lang.NumberFormatException: For input string: " 2e5d98 "
ClassCastException类型转换异常
//产生原因:将引用转换成错误的"原类型"publicstaticvoidmain(String[] args){
Book book =newBook();
Account account =newAccount();// 由于Object是所有类的父类,所以book对象或account对象均可以传入// dosth(book);dosth(account);}// 参数为Object父类,传入时,发生向上转型,导致原对象的类型被隐藏publicstaticvoiddosth(Object obj){//为了可以调用到原对象的独有方法,必须“向下转型”
Book book =(Book)obj;
book.bookList();}}
ParseException转换异常
//产生原因:转换的字符串与SimpleDateFormat的日期格式不匹配导致
Date now =newDate();// 转换成字符串
SimpleDateFormat sdf =newSimpleDateFormat("yyyy年MM月dd日");
String nowStr = sdf.format(now);
System.out.println("当前日期:"+ nowStr);// 转换成日期对象
String str ="2020年01月";// 如果格式字符串与原字符串不匹配,会产生转换异常
Date date = sdf.parse(str);
System.out.println("指定日期:"+ date);//Exception in thread "main" java.text.ParseException: Unparseable date: "2020年01月"
StringIndexOutOfBoundsException 字符串下标越界异常
//产生原因:字符串的下标越界
String str ="zzzzzzzzzzzzzzzzzzzzzzzzzz";
System.out.println(str.charAt(110));//Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 110