Java 异常体系

目录

1. 🔍异常的基本语法

2. 🔍异常处理流程 

3. 🔍抛出异常 -- throw 和 throws 

3.1 🎈throw

3.2 🎈throws 

4. Java 异常体系 


1. 🔍异常的基本语法

 注意:

1. try 代码块中放的是可能出现异常的代码

✨ 2. catch 代码块中放的是出现异常后的处理行为,一个代码中可以搭配多个catch来使用

✨ 3. finally 代码块中的代码用于无论是否会产生异常,都会被执行。(程序中类型资源关闭,文件关闭,数据库连接关闭等操作都放在finally 代码块中)

✨ 4. catch 和 finally 根据情况可以有也可以没有,但是try 语句不能省

✨ 5. Exception 是所有运行时异常的共同父类

public class A{
    public static void main(String[] args) {
        int a = test();
        System.out.println(a);
    }
    public static int test() {
        try {
            System.out.println("1");
            return 1;
        } catch (Exception e) {
            System.out.println("2");
            return 2;
        } finally {
            System.out.println("3333");
            return 3;
        }
    }
}

输出:
1
3333
3

2. 🔍异常处理流程 

public class A{
    public static void main(String[] args) {
        try {
            func();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static void func() {
        int[] arr = {1, 2, 3};
        System.out.println(arr[100]);
    }
}

// 直接结果
java.lang.ArrayIndexOutOfBoundsException: 100 at demo02.Test.func(Test.java:18)
 at demo02.Test.main(Test.java:9) after try catch

 ✨1. 程序先执行try中的代码

✨2. 如果try 中的代码出现异常, 就会结束 try 中的代码, 看和 catch 中的异常类型是否匹配

✨3. 如果找到匹配的异常类型, 就会执行 catch 中的代码

✨4. 如果没有找到匹配的异常类型, 就会将异常向上传递到上层调用者

✨5. 无论是否找到匹配的异常类型, fifinally 中的代码都会被执行到(在该方法结束之前执行)

✨6. 如果上层调用者也没有处理的了异常, 就继续向上传递

✨7. 一直到 main 方法也没有合适的代码处理异常, 就会交给 JVM 来进行处理, 此时程序就会异常终止.

3. 🔍抛出异常 -- throw 和 throws 

throw :用在方法内部,手工抛出异常,人工产生异常对象

throws:用在方法上声明,表示当前方法可能会出现的异常 

3.1 🎈throw

public class A{
    public static void main(String[] args) {
        int ret = chuFa(10,0);
        System.out.println(ret);
    }
    public static int chuFa(int a,int b) {
        if(b == 0) {
            throw new ArithmeticException("不能除0");
        }
        System.out.println("正常代码");
        return a / b;
    }
}
输出:
Exception in thread "main" java.lang.ArithmeticException: 不能除0
	at arrays.A.chuFa(A.java:17)
	at arrays.A.main(A.java:12)

3.2 🎈throws 

✨ throws 抛出异常方法1: 

public class A{
    public static void main(String[] args) {
        File file = new File("/test.txt");
        Scanner sc = null;
        try {
            sc = new Scanner(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            sc.close();
        }
    }
}

✨ throws 抛出异常方法2:  

public class A{
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("/test.txt");
        Scanner sc = new Scanner(file);
    }
}

4. Java 异常体系 

✨ 非受查异常:程序可以不用显示处理该异常

✨ 受查异常:程序必须显示 try ... catch ... finally 或 throws 向上抛出异常

✨ 受查异常:Throwable , Exception, IOException, ClassNotFoundException, CloneNotSupportedException, EOFException, FileBotFoundException, MalformedURLException, UnknownHostException 

✨ 非受查异常:Error, RuntimeException, ArithmeticException, ClassCastException, illgalArgumentExcrption, illgalStateException, IndexOutOfBoundsException, NoSuchElementException, NullPointerException

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值