try catch简单应用

不加try catch

package com.it.test;

public class TryCatchTest {
    public static void main(String[] args) {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry(){
        int number = 0;
        int numberReturn = 1;
        numberReturn = 10 / number;
        System.out.println("报错了");
        return numberReturn;
    }
}

结果

加了try catch

package main.java.test;

public class TryCatchTest {
    public static void main(String[] args) {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry(){
        int number = 0;
        int numberReturn = 1;
        try{
            numberReturn = 10 / number;
        }catch (Exception e){
            e.getStackTrace();
        }
        System.out.println("报错了");
        return numberReturn;
    }
}

结果

catch里有代码

package main.java.test;

public class TryCatchTest {
    public static void main(String[] args) {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry(){
        int number = 0;
        int numberReturn = 1;
        try{
            numberReturn = 10 / number;
        }catch (Exception e){
            numberReturn = 2;     //对numberReturn再次赋值
            System.out.println("catch里的代码走了");
            e.getStackTrace();
        }
        System.out.println("报错了");
        return numberReturn;
    }
}

结果

catch里再throw 一个Exception

package main.java.test;

public class TryCatchTest {
    public static void main(String[] args) throws Exception {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry() throws Exception {
        int number = 0;
        int numberReturn = 1;
        try{
            numberReturn = 10 / number;
        } catch (Exception e){
            numberReturn = 3;
            throw new Exception();
//            System.out.println("catch里的代码走了");//在catch后的throw里不能再加代码了,idea会自动检测unreachable statement
        }
        System.out.println("报错了");
        return numberReturn;
    }

}

结果

catch里有return====直接在catch的return里返回

package main.java.test;

public class TryCatchTest {
    public static void main(String[] args) throws Exception {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry() throws Exception {
        int number = 0;
        int numberReturn = 1;
        try{
            numberReturn = 10 / number;
        } catch (Exception e){
           numberReturn = 3;
           return numberReturn;
        }
        System.out.println("报错了");
        numberReturn = 4;
        return numberReturn;
    }

}

结果

try catch finally

package main.java.test;

public class TryCatchTest {
    public static void main(String[] args) throws Exception {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry() throws Exception {
        int number = 0;
        int numberReturn = 1;
        try{
            numberReturn = 10 / number;
        } catch (Exception e){
           numberReturn = 3;
        } finally {
            numberReturn = 4;
            System.out.println("finally代码被执行");
        }
        numberReturn = 5;
        return numberReturn;
    }

}

结果

catch里有return,finally里没有

package main.java.test;

public class TryCatchTest {
    public static void main(String[] args) throws Exception {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry() throws Exception {
        int number = 0;
        int numberReturn = 1;
        try{
            numberReturn = 10 / number;
        } catch (Exception e){
           numberReturn = 3;
            return numberReturn;
        } finally {
            numberReturn = 4;
            System.out.println("finally代码被执行");
        }
        numberReturn = 5;
        return numberReturn;
    }

}

结果

catch和finally里都有return

package main.java.test;

public class TryCatchTest {
    public static void main(String[] args) throws Exception {
        int i = testTry();
        System.out.println("值为" + i);
    }

    public static int testTry() throws Exception {
        int number = 0;
        int numberReturn = 1;
        try {
            numberReturn = 10 / number;
        } catch (Exception e) {
            numberReturn = 3;
            return numberReturn;
        } finally {
            numberReturn = 4;
            System.out.println("finally代码被执行");
            return numberReturn;
        }
    }

}

 结果

总结

try{
    //业务代码
}catch(Exception e){
    logger.error("报错",e)
}finally{
    
}

1.加了try catch finally之后无论程序是否报错,都会走finally,

程序不报错,try业务代码中有return,finally中也有return,则最后会走finally的return

程序报错,try 业务代码中有return,catch中有return,finally中也有return,则最后会走finally中的return

2.加了try catch之后,

程序报错,try业务代码中有return,catch中有return,则会走catch中的return

程序不报错,try业务代码中有return,catch中有return,则会走try中的return

程序报错,try业务代码中没有return,catch中有return,try catch最后有return,则会走catch中的return

没报错,try业务代码中没有return,catch中有return,try catch最后有return,则会走try catch之后的return

 3.try 中有return,catch中有return,finally中有return,程序不报错,最后会返回finally中的return,finally代码会执行

4.try 中有return,catch中有return,finally中有return,程序报错,catch和finally中的代码会执行,最终会走finally中的return

 5.try 中有return,catch中有return,finally中无return,程序报错,catch和finally中的代码会执行,最终会在finally执行完之后,一起在catch中返回return

 6.try 中有return,catch中有return,finally中无return,程序不报错,finally中的代码会执行,最终会在finally中执行完之后,一起在try中的return

 

如遇到知识性的错误请留言

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值