try-catch-finally 捕获异常不在catch里抛出;循环遍历对象生成任务,捕获异常对象不抛出,不影响其他正常对象生成任务

场景:一个模板绑定多个对象,要对每个对象生成任务。捕获生成任务过程中的异常,但是不抛出,只是用日志记录。这样做目的:循环遍历对象生成任务时,异常对象数据生成任务时发生异常只是导致自己生成任务失败,但不会影响到其他正常对象数据生成任务。例如对象1数据异常,那就捕获对象1的异常信息但不抛出(一旦抛出就会导致后面的对象生成任务失败),只是对象1生成不了任务。对象2,对象3是正常的,还是可以正常生成任务。

一、举例

//1、try-catch 直接捕获异常不在catch里抛出,会执行try-catch之外的代码
public class NullPointerExample {
    public static void main(String[] args) {
        String str = null;

        try {
            // 尝试执行可能抛出空指针异常的代码
            System.out.println(str.length()); // 这里会抛出NullPointerException
            createTask();//不会执行生成任务的代码
        } catch (Exception e) {
            // 捕获到空指针异常后,在这里处理异常
            System.out.println("捕获到空指针异常: " + e.getMessage());
        }
        // try-catch之外的代码
        System.out.println("在catch里异常没有被抛出,程序能继续执行...");
    }
}

在这里插入图片描述

//2、try-catch直接捕获异常在catch里抛出,不会执行try-catch之外的代码
public class NullPointerExample {
    public static void main(String[] args) {
        String str = null;

        try {
            // 尝试执行可能抛出空指针异常的代码
            System.out.println(str.length()); // 这里会抛出NullPointerException
            createTask();//不会执行生成任务的代码
        } catch (Exception e) {
            // 捕获到空指针异常后,在这里处理异常
            System.out.println("捕获到空指针异常: " + e.getMessage());
            throw e; //这里直接抛出异常,那么try-catch之外的代码就不能执行了
        }
        // try-catch之外的代码
        System.out.println("在catch里异常被抛出,程序不能继续执行...");
    }
}

在这里插入图片描述

//3、在catch里异常被抛出,finally里的代码能执行,try-catch-finally之外的后续代码不能执行
public class NullPointerExample {
    public static void main(String[] args) {
        String str = null;

        try {
            // 尝试执行可能抛出空指针异常的代码
            System.out.println(str.length()); // 这里会抛出NullPointerException
            createTask();//不会执行生成任务的代码
        } catch (Exception e) {
            // 捕获到空指针异常后,在这里处理异常
            System.out.println("捕获到空指针异常: " + e.getMessage());
            throw e; //这里直接抛出异常,那么try-catch-finally之外的后续代码不能执行
        } finally {
            System.out.println("在catch里异常被抛出,finally里的代码能执行,try-catch-finally之外的后续代码不能执行...");
        }
        // try-catch-finally之外的后续代码
        System.out.println("在catch里异常被抛出,程序不能继续执行...");
    }
}

在这里插入图片描述

//4.在catch里异常不被抛出,finally里的代码能执行,try-catch-finally之外的后续代码能执行
public class NullPointerExample {
    public static void main(String[] args) {
        String str = null;

        try {
            // 尝试执行可能抛出空指针异常的代码
            System.out.println(str.length()); // 这里会抛出NullPointerException
            createTask();//不会执行生成任务的代码
        } catch (Exception e) {
            // 捕获到空指针异常后,在这里处理异常
            System.out.println("捕获到空指针异常: " + e.getMessage());
        } finally {
            System.out.println("在catch里异常不被抛出,finally里的代码能执行,try-catch-finally之外的后续代码能执行...");
        }
        // try-catch-finally之外的后续代码
        System.out.println("在catch里异常不被抛出,程序能继续执行...");
    }
}

在这里插入图片描述

二.、总结

1、无论catch里的异常有没有被抛出,finally里的代码都能执行;
2、catch里的异常被抛出,try-catch之外或者try-catch-finally之外的代码都不能执行;
3、catch里的异常不被抛出,try-catch之外或者try-catch-finally之外的代码都能执行,例如循环遍历对象生成任务,异常对象捕获但不抛出异常,那该异常对象就不会生成任务,但是不影响其他正常对象生成任务;
4、try-catch 之间的代码:发生异常就会被捕获,发生异常地方处后面的代码不会被执行,
( 例如: System.out.println(str.length()); //发生异常,后面的 createTask(); 就不会执行生成任务的代码)。

三、循环遍历对象捕获异常举例

//循环遍历对象 举例1:捕获异常没有抛出,person3数据异常,只会导致person3 在try-catch之间的代码不能执行,person1和person2还是能执行try-catch之间的代码;三个都能执行try-catch之外或者try-catch-finally之外的代码。
public class NullPointerExample {
    public static void main(String[] args) {
        Person person1 = new Person();
        Person person2 = new Person();
        Person person3 = new Person();
        person1.setId(1);
        person1.setName("小米");
        person2.setId(2);
        person2.setName(null);
        person3.setId(3);
        person3.setName("小飞");
        List<Person> people = new ArrayList<>();
        people.add(person1);
        people.add(person2);
        people.add(person3);
       for (Person person : people){
        try {
            // 尝试执行可能抛出空指针异常的代码
            System.out.println("person的姓名长度是:"+person.getName().length()); // 这里会抛出NullPointerException
            System.out.println("try-catch之间的代码执行结果:person的id是"+person.getId()+";"+"person的名称是"+person.getName());  //捕获异常后不会执行到这一步
        } catch (Exception e) {
            // 捕获到空指针异常后,在这里处理异常
            System.out.println("捕获到空指针异常: " + e.getMessage());
        } finally {
            System.out.println("在catch里异常不被抛出,finally里的代码能执行,try-catch-finally之外的后续代码能执行...");
        }
        // try-catch-finally之外的后续代码
        System.out.println("try-catch或try-catch-finally之外的后续代码:" + person);
        System.out.println("---------------------------");
       }
    }
}

在这里插入图片描述

//循环遍历对象 举例2: 捕获异常直接抛出,person2数据异常,导致person2和后面的person3都不能正常执行try-catch之间的代码、try-catch或try-catch-finally之外的后续代码。
public class NullPointerExample {
    public static void main(String[] args) {
        Person person1 = new Person();
        Person person2 = new Person();
        Person person3 = new Person();
        person1.setId(1);
        person1.setName("小米");
        person2.setId(2);
        person2.setName(null);
        person3.setId(3);
        person3.setName("小飞");
        List<Person> people = new ArrayList<>();
        people.add(person1);
        people.add(person2);
        people.add(person3);
       for (Person person : people){
        try {
            // 尝试执行可能抛出空指针异常的代码
            System.out.println("person的姓名长度是:"+person.getName().length()); // 这里会抛出NullPointerException
            System.out.println("try-catch之间的代码执行结果:person的id是"+person.getId()+";"+"person的名称是"+person.getName());  //捕获异常后不会执行到这一步
        } catch (Exception e) {
            // 捕获到空指针异常后,在这里处理异常
            System.out.println("捕获到空指针异常: " + e.getMessage());
            throw e;
        } finally {
            System.out.println("在catch里异常被抛出,finally里的代码能执行,try-catch-finally之外的后续代码不能执行...");
        }
        // try-catch-finally之外的后续代码
        System.out.println("try-catch或try-catch-finally之外的后续代码:" + person);
        System.out.println("---------------------------");
       }
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值