JAVA基础--异常情况

JAVA基础--异常情况


前言:

任何JAVA代码执行时都有可能出现异常情况的抛出。一般在异常方法内部有两种情况处理异常。

  1. 通过trycatch处理异常
  2. 通过throws向外层方法抛出异常

对不处理异常情况的代码情况进行代码实践,代码实践两部分

  1. 不处理异常情况
  2. 多线程下出现一个线程抛出运行时异常

一、通过trycatch处理异常

执行代码:

1、Catch类型

含抛出异常方法方法。tryCatch方法内设置抛出异常后执行的输出日志埋点

public class Catch {

    public int tryCatch(){
        try{
            executeException();
            System.out.println("抛出异常后try里面执行");
        }catch (Exception exception){
            System.out.println("抛出异常后catch里面执行");
        }finally {
            System.out.println("抛出异常后finally里面执行");
        }
        System.out.println("抛出异常后catch里面外面执行");
        return 1;
    }

    public void executeException() throws Exception{
        throw new RuntimeException();
    }
}

2、main类型

用于执行Catch类型的tryCatch方法,设置判断抛出异常方法tryCatch通过处理异常后能否正常往下执行提供输出日志埋点,也作为程序启动类。

public class main {
    public static void main(String[] args) {
        Catch test = new Catch();
        int i = test.tryCatch();
        System.out.println("catch后会继续往下执行,方法结果为:"+i);
    }
}

执行结果: 

从日志埋点可知,发生抛出异常后经过try、catch处理。

  • try内部抛出异常后try块异常方法后的代码不执行
  • catch内部代码执行
  • finally内部代码执行
  • try、catch、finally块后方法代码正常执行、正常返回。
  • 方法外部上层方法执行不受影响 

二、通过throws向外层方法抛出异常

执行代码:

1、Catch类型

含抛出异常方法。tryException方法内设置抛出异常后执行的输出日志埋点。方法使用throw Exception抛出异常。

public class Catch {

    public int throwException() throws Exception{
        executeException();
        System.out.println("抛出异常后方法里面执行");
        return 1;
    }

    public void executeException(){
        throw new RuntimeException();
    }
}

2、main类型

仅在第一次遍历时用于执行Catch类型的tryException方法,程序有两次遍历。设置判断抛出异常方法tryException通过直接抛出异常后能否正常往下执行提供输出日志埋点,也作为程序启动类。方法使用throw Exception抛出异常。

public class main {
    public static void main(String[] args)throws Exception{
        Catch test = new Catch();
        for(int i=0;i<2;i++){
            if(i==0)test.throwException();
            System.out.println("throwException后会继续往下执行,方法结果为:"+i);
        }
    }
}

执行结果: 

都抛出异常未进行异常处理,程序停止执行并抛出异常。但被调用方法抛出异常,外部调用方法提供trycatch处理程序则正常往下执行,例子参见第一部分:一、通过trycatch处理异常

三、不处理异常情况

执行代码:

1、Catch类型

含抛出运行时异常方法,此处使用1/0运算抛出的异常。tryException方法内设置抛出异常后执行的输出日志埋点

public class Catch {

    public int throwException(){
        int i=1/0;
        System.out.println("抛出异常后方法里面执行");
        return 1;
    }
}

2、main类型

仅在第一次遍历时执行Catch类型的tryException方法,程序有两次遍历。设置判断抛出异常方法tryException通过直接抛出异常后能否正常往下执行提供输出日志埋点,也作为程序启动类。

public class main {
    public static void main(String[] args){
        Catch test = new Catch();
        for(int i=0;i<2;i++){
            if(i==0)test.throwException();
            System.out.println("throwException后会继续往下执行,方法结果为:"+i);
        }
    }
}

执行结果: 

 未进行异常处理,程序停止执行并抛出异常(单线程下)

四、多线程下出现一个线程抛出运行时异常

执行代码:

1、Mythead类型

设置线程类型,每个线程共用一个整型对象i。当i=0时该线程必报异常。

public class MyThread extends Thread {

    public static Integer i=3;
    @Override
    public void run() {
        synchronized (i){
            System.out.println(Thread.currentThread().getName() + "正在执行。。。当前共享i="+i--);
            int t=8/i;
        }
    }
}

2、main类型

内含线程池,线程池执行6个MyThread线程。应该MyThread中i=3总会出现有一线程执行i=0情况。

public class main {
    public static void main(String[] args){
        ExecutorService pool = Executors.newFixedThreadPool(10);
        MyThread t1=new MyThread();
        MyThread t2=new MyThread();
        MyThread t3=new MyThread();
        MyThread t4=new MyThread();
        MyThread t5=new MyThread();
        MyThread t6=new MyThread();
        //将线程放入池中进行执行
        pool.execute(t1);
        pool.execute(t2);
        pool.execute(t3);
        pool.execute(t4);
        pool.execute(t5);
        pool.execute(t6);
    }
}

 执行结果: 

一个线程报异常不影响其他线程的正常执行,同理对于进程也一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅尝即止何来突破

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值