比如:数组索引越界异常、空指针异常、日期格式化异常,等....
- Exception:异常
- Java中异常继承的根类是:Throwable。Throwable是根类,不是异常类。
- Exception才是异常类,它才是开发中代码在编译或者执行的过程中可能出现的错误,它是需要提前处理的,以便程序更健壮!
- 异常体系的最上层父类是Exception。
- 异常分为两类:编译时异常、运行时异常。
- 编译时异常:没有继承RuntimeException的异常,直接继承于Exception。编译阶段就会错误提示 / 报错,必须要手动处理,否则代码报错不通过。编译时异常是为了提醒程序员。
- 运行时异常:继承了RuntimeException,RuntimeException本身和子类。编译阶段不会报错 / 出现异常提醒,运行时出现的异常。运行时异常是代码出错而导致程序出现的问题。
package com.gch.d3_exception;/**目标:异常的概念和体系。*/
public class ExceptionDemo {public static void main(String[] args) {int[] arr = {10, 20, 40};System.out.println(arr[0]);System.out.println(arr[1]);System.out.println(arr[2]);System.out.println(arr[3]); // ArrayIndexOutOfBoundsExceptionSystem.out.println("-----------程序截止---------");}
}
package com.gch.d3_exception;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class ExceptionDemo1 {public static void main(String[] args) throws ParseException {// 编译时异常(在编译阶段,必须要手动处理,否则代码报错)String time = "2030年1月1日";SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");Date date = sdf.parse(time);System.out.println(date); // Tue Jan 01 00:00:00 CST 2030// 运行时报错(在编译阶段是不需要处理的,是代码运行时出现的异常)int[] arr = {1, 2, 3, 4, 5};System.out.println(arr[10]); // ArrayIndexOutOfBoundsException}
}
package com.gch.d4_exception_runtimeException;
/**拓展: 常见的运行时异常。(面试题)运行时异常的概念:继承自RuntimeException的异常或者其子类,编译阶段是不会出错的,它是在运行时阶段可能出现的错误,运行时异常编译阶段可以处理也可以不处理,代码编译都能通过!!1.数组索引越界异常: ArrayIndexOutOfBoundsException。2.空指针异常 : NullPointerException。直接输出没有问题。但是调用空指针的变量的功能就会报错!!3.类型转换异常:ClassCastException。4.迭代器遍历没有此元素异常:NoSuchElementException。5.算术异常:ArithmeticException。6.数字转换异常:NumberFormatException。小结:运行时异常继承了RuntimeException ,编译阶段不报错,运行时才可能会出现错误!*/
public class ExceptionDemo {public static void main(String[] args) {System.out.println("程序开始。。。。。。");/** 1.数组索引越界异常: ArrayIndexOutOfBoundsException。*/int[] arr = {1, 2, 3};System.out.println(arr[2]);// System.out.println(arr[3]); // 运行出错,程序终止/** 2.空指针异常 : NullPointerException。直接输出没有问题。但是调用空指针的变量的功能就会报错!! */String name = null;System.out.println(name); // null// System.out.println(name.length()); // 运行出错,程序终止/** 3.类型转换异常:ClassCastException。 */Object o = 23;// String s = (String) o; // 运行出错,程序终止/** 5.数学操作 / 算术异常:ArithmeticException。 *///int c = 10 / 0;/** 6.数字转换异常: NumberFormatException。 *///String number = "23";String number = "23aabbc";Integer it = Integer.valueOf(number); // 运行出错,程序终止System.out.println(it + 1);System.out.println("程序结束。。。。。");}
}
package com.gch.d5_exception_javac;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/**目标:常见的编译时异常认识。编译时异常:继承自Exception的异常或者其子类,没有继承RuntimeException"编译时异常是编译阶段就会报错",必须程序员编译阶段就处理的。否则代码编译就报错!!编译时异常的作用是什么:是担心程序员的技术不行,在编译阶段就爆出一个错误, 目的在于提醒!提醒程序员这里很可能出错,请检查并注意不要出bug。编译时异常是可遇不可求。遇到了就遇到了呗。了解: */
public class ExceptionDemo { // 解析异常public static void main(String[] args) throws ParseException {String date = "2015-01-12 10:23:21";// 创建一个简单日期格式化类:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 解析字符串时间成为日期对象Date d = sdf.parse(date);System.out.println(d); // Mon Jan 12 10:23:21 CST 2015}
}
package com.gch.d8_handle_runtime;import java.sql.SQLOutput;
import java.util.Scanner;/**需求:需要输入一个合法的价格为止 要求价格大于0*/
public class Test2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(true){try {System.out.println("请您输入价格:");String priceStr = sc.nextLine();// 转换成Double类型的价格double price = Double.valueOf(priceStr);// 判断价格是否大于0if(price > 0){System.out.println("定价:" + price);break;}else{System.out.println("价格必须是正数~~~");}} catch (NumberFormatException e) {System.out.println("程序有误!");
// throw new RuntimeException(e);}}}
}