|NO.Z.00067|——————————|BigDataEnd|——|Java&集合类库.V03|——|Java.v03|异常机制.v03|异常捕获|实现|

一、异常捕获实现
### --- 注意事项

——>        a.当需要编写多个catch分支时,切记小类型应该放在大类型的前面;
——>        b.懒人的写法:
——>        catch(Exception e) {}
——>        c.finally通常用于进行善后处理,如:关闭已经打开的文件等。
### --- 执行流程

——>        try {
——>            a;
——>            b; - 可能发生异常的语句
——>            c;
——>        }catch(Exception ex) {
——>            d;
——>        }finally {
——>            e;
——>        }
——>        当没有发生异常时的执行流程:a b c e;
——>        当发生异常时的执行流程:a b d e;
二、编程代码
package com.yanqi.task16;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionCatchTest {

    public static void main(String[] args) {

        // 创建一个FileInputStream类型的对象与d:/a.txt文件关联,打开文件
        FileInputStream fis = null;
        try {
            System.out.println("1");
            // 当程序执行过程中发生了异常后直奔catch分支进行处理
            fis = new FileInputStream("d:/a.txt");
            System.out.println("2");
        } catch (FileNotFoundException e) {
            System.out.println("3");
            e.printStackTrace();
            System.out.println("4");
        }
        // 关闭文件
        try {
            System.out.println("5");
            fis.close();
            System.out.println("6");
        } /*catch (Exception e) {
            e.printStackTrace();
        }*/ catch (IOException e) {
            System.out.println("7");
            e.printStackTrace();
            System.out.println("8");
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("世界上最真情的相依就是你在try我在catch,无论你发神马脾气我都默默承受并静静的处理,到那时再来期待我们的finally!");
        // 当程序执行过程中没有发生异常时的执行流程:1 2  5 6  世界上...
        // 当程序执行过程中发生异常又没有手动处理空指针异常时的执行流程:1 3 4  5  空指针异常导致程序终止
        // 当程序执行过程中发生异常并且手动处理空指针异常时的执行流程: 1 3 4 5 世界上...

        // 手动处理异常和没有处理的区别:代码是否可以继续向下执行
    }
}
三、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58506:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task16.ExceptionCatchTest
1
3
4
5
世界上最真情的相依就是你在try我在catch,无论你发神马脾气我都默默承受并静静的处理,到那时再来期待我们的finally!
java.io.FileNotFoundException: d:\a.txt (系统找不到指定的文件。)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
    at com.yanqi.task16.ExceptionCatchTest.main(ExceptionCatchTest.java:16)
java.lang.NullPointerException
    at com.yanqi.task16.ExceptionCatchTest.main(ExceptionCatchTest.java:26)

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示