异常的概念
异常指的是程序执行过程中,出现的非正常情况,最终会导致JVM的非正常停止,在Java中,异常是一个类,产生异常就是创建一个异常对象并抛出,Java处理异常的方式就是中断处理。
异常体系
异常的根类是java.lang.Throwable
有两个子类:java.lang.Error 和 java.lang.Exception
Exception: 编译期异常:写代码的时候会提示的异常
RuntimeException:程序运行时异常
Error:程序错误,需修改源代码
编译期异常的两种处理方式
1、把异常直接抛出给虚拟机处理,程序中断
public class Demo01Exception {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.parse("2021-0327");
System.out.println("程序后续处理");
}
}
运行结果:虚拟机直接抛出异常,不再执行后续代码
Exception in thread “main” java.text.ParseException: Unparseable date: “2021-0327”
at java.text.DateFormat.parse(DateFormat.java:366)
at com.javase.demo01.Exception.Demo01Exception.main(Demo01Exception.java:13)
2、把异常捕获进行处理,并执行后续程序
public class Demo01Exception {
public static void main(String[] args) /*throws ParseException*/ {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try{
date = sdf.parse("2021-0327");
}catch(ParseException e){
System.out.println(e);
}
System.out.println("程序后续处理");
}
}
运行结果:捕获异常,并执行后续代码
java.text.ParseException: Unparseable date: “2021-0327”
程序后续处理
运行时异常的处理
运行时异常是指程序编译时不会提示异常,但在程序执行时发生异常,可以用捕获异常进行处理
例如数组的索引越界异常
public class Demo02RuntimeException {
public static void main(String[] args) {
int[] i = {1,2,3};
try{
System.out.println(i[3]);
}catch(ArrayIndexOutOfBoundsException e){ //如果不知道具体异常,可以使用异常的统一父类Exception e
System.out.println(e);
}
System.out.println("后续代码执行");
}
}
运行结果:
java.lang.ArrayIndexOutOfBoundsException: 3
后续代码执行
Error的异常
提示Error的程序,无法进行异常处理,只能修改源代码
例如:内存溢出错误OutOfMemoryError
public class Demo03Error {
public static void main(String[] args) {
int[] a = new int[1024*1024*1024];
}
}
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at com.javase.demo01.Exception.Demo03Error.main(Demo03Error.java:9)
异常中的关键字
throw关键字
throw关键字可以在指定的方法中抛出指定的异常
格式:
throw new XXXException(“异常的原因”);
注意事项:
1、throw关键字必须写在方法内部
2、throw new的对象必须是Exception或者它的子类对象
3、throw new 如果创建的是RuntimeException或者是 它的子类对象,默认由JVM打印异常对象,并中断程序
4、如果throw new 创建的是编译时异常,需要throws抛出这个异常,或者是try…catch这个异常
public class Demo04Throws {
public static void main(String[] args) {
int[] arr = {1,2,3};
printArray(arr,3);
}
public static void printArray(int[] arr,int index){
if(arr == null){
throw new NullPointerException("传入数组为空");
}
if(index < 0 || index>arr.length-1){
throw new ArrayIndexOutOfBoundsException("数组索引越界");//该异常为RuntimeException,可以由JVM默认处理
}
}
}
throw的应用
objects类的静态方法:public static T requireNonNull(T obj){};
用于检查传入的对象是否为空
public class Demo04Objects {
public static void main(String[] args) {
Object obj = null;
method(obj);
}
public static void method(Object obj){
Objects.requireNonNull(obj);
}
}
会抛出空指针异常
requireNonNull方法的源码:
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
Throws关键字
1、 throws关键字必须写在方法声明处,声明的异常必须是Exception或者Exception的子类
2、如果方法内部抛出多个异常,throws后面也必须声明多个异常
3、如果调用声明抛出异常的方法,那么调用这个方法的地方就必须要处理这个异常:要么继续用throws抛出异常给JVM处理,要么使用try…catch捕获异常处理
1、使用Throws抛出异常的处理
public class Demo05Throws {
public static void main(String[] args) throws FileNotFoundException, IOException{
testFile("c:\\a",".txt");
}
public static void testFile(String direst,String ends) throws FileNotFoundException, IOException{
if(!direst.equals("c:\\a")){
throw new FileNotFoundException("传递文件路径不对");
}
if(!ends.endsWith(".txt")) {
throw new IOException("文件的后缀名不对");
}
System.out.println("路径正常,可下载文件");
}
}
未完待续。。。