try{...}catch(){...}finally{...}语句你真的理解吗?

简介: try{...}catch(){...}finally{...}语句你真的理解吗?
/**
 * @desc: 探究try...catch...finally语句块
 * 六种模式:
 * (1)try{ ... } catch(){ ... }finally{ ... }  ... return;
 * (2)try{ ... return;}catch(){ ... } finally{ ... }  ... return;
 * (3)try{ ... } catch(){ ... return;} finally{ ... } ... return;
 * (4)try{ ... return;}catch(){ ... } finally{ ... return;}
 * (5)try{ ... } catch(){ ... return;}finally{ ... return;}
 * (6)try{ ... return;}catch(){ ... return;} finally{ ... return;}
 * @author: YanMingXin
 * @create: 2021/8/10-10:16
 **/
public class TryTest {
    /**
     * 常量1
     */
    public static final String s1 = "Hello";
    /**
     * 常量2
     */
    public static final String s2 = "World";
    /**
     * try{ ... } catch(){ ... }finally{ ... }  ... return;
     *
     * @return
     */
    public static String model1() {
        String s = new String();
        try {
            s = s1 + s2;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally " + s);
        }
        return s;
    }
    public static String model1HaveException() {
        String s = new String();
        try {
            s = s1 + s2;
            int i = 2 / 0;
        } catch (Exception e) {
            System.err.println("model1异常信息");
            e.printStackTrace();
        } finally {
            System.out.println("finally " + s);
        }
        return s;
    }
    /**
     * try{ ... return;}catch(){ ... } finally{ ... }  ... return;
     *
     * @return
     */
    public static String model2() {
        String s = new String();
        try {
            s = s1 + s2;
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally " + s);
        }
        return s;
    }
    public static String model2HaveException() {
        String s = new String();
        try {
            s = s1 + s2;
            int i = 2 / 0;
            return s;
        } catch (Exception e) {
            System.err.println("model2异常信息");
            e.printStackTrace();
        } finally {
            System.out.println("finally " + s);
        }
        return s;
    }
    /**
     * try{ ... } catch(){ ... return;} finally{ ... } ... return;
     *
     * @return
     */
    public static String model3() {
        String s = new String();
        try {
            s = s1 + s2;
            //return s;  与最后的return冲突
        } catch (Exception e) {
            e.printStackTrace();
            return s;
        } finally {
            System.out.println("finally " + s);
        }
        return s;
    }
    public static String model3HaveException() {
        String s = new String();
        try {
            s = s1 + s2;
            int i = 1 / 0;
            //return s;  与最后的return冲突
        } catch (Exception e) {
            System.err.println("model3异常信息");
            e.printStackTrace();
            return s;
        } finally {
            System.out.println("finally " + s);
        }
        return s;
    }
    /**
     * try{ ... return;}catch(){ ... } finally{ ... return;}
     *
     * @return
     */
    public static String model4() {
        String s = new String();
        try {
            s = s1 + s2;
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally " + s);
            return s;
        }
        //return s; 与finally的return冲突
    }
    public static String model4HaveException() {
        String s = new String();
        try {
            s = s1 + s2;
            int i = 5 / 0;
            return s;
        } catch (Exception e) {
            System.err.println("model4异常信息");
            e.printStackTrace();
        } finally {
            System.out.println("finally " + s);
            return s;
        }
        //return s; 与finally的return冲突
    }
    /**
     * try{ ... } catch(){ ... return;}finally{ ... return;}
     *
     * @return
     */
    public static String model5() {
        String s = new String();
        try {
            s = s1 + s2;
        } catch (Exception e) {
            e.printStackTrace();
            return s;
        } finally {
            System.out.println("finally " + s);
            return s;
        }
    }
    public static String model5HaveException() {
        String s = new String();
        try {
            s = s1 + s2;
            int i = 2 / 0;
        } catch (Exception e) {
            System.err.println("model5异常信息");
            e.printStackTrace();
            return s;
        } finally {
            System.out.println("finally " + s);
            return s;
        }
    }
    /**
     * try{ ... return;}catch(){ ... return;} finally{ ... return;}
     *
     * @return
     */
    public static String model6() {
        String s = new String();
        try {
            s = s1 + s2;
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            return s;
        } finally {
            System.out.println("finally " + s);
            return s;
        }
    }
    public static String model6HaveException() {
        String s = new String();
        try {
            s = s1 + s2;
            int i = 2 / 0;
            return s;
        } catch (Exception e) {
            System.err.println("model6异常信息");
            e.printStackTrace();
            return s;
        } finally {
            System.out.println("finally " + s);
            return s;
        }
    }
    public static void main(String[] args) {
        System.out.println("model1无异常:");
        System.out.println(model1());
        System.out.println("model1有异常:");
        System.out.println(model1HaveException());
        System.out.println("model2无异常:");
        System.out.println(model2());
        System.out.println("model2有异常:");
        System.out.println(model2HaveException());
        System.out.println("model3无异常:");
        System.out.println(model3());
        System.out.println("model3有异常:");
        System.out.println(model3HaveException());
        System.out.println("model4无异常:");
        System.out.println(model4());
        System.out.println("model4有异常:");
        System.out.println(model4HaveException());
        System.out.println("model5无异常:");
        System.out.println(model5());
        System.out.println("model5有异常:");
        System.out.println(model5HaveException());
        System.out.println("model6无异常:");
        System.out.println(model6());
        System.out.println("model6有异常:");
        System.out.println(model6HaveException());
    }
}

运行结果:

model1无异常:
finally HelloWorld
HelloWorld
model1有异常:
finally HelloWorld
HelloWorld
model2无异常:
finally HelloWorld
HelloWorld
model2有异常:
finally HelloWorld
HelloWorld
model3无异常:
finally HelloWorld
HelloWorld
model3有异常:
finally HelloWorld
HelloWorld
model4无异常:
finally HelloWorld
HelloWorld
model4有异常:
finally HelloWorld
HelloWorld
model5无异常:
finally HelloWorld
HelloWorld
model5有异常:
finally HelloWorld
HelloWorld
model6无异常:
finally HelloWorld
HelloWorld
model6有异常:
finally HelloWorld
HelloWorld
model1异常信息
java.lang.ArithmeticException: / by zero
  at org.ymx.test0810.TryTest.model1HaveException(TryTest.java:49)
  at org.ymx.test0810.TryTest.main(TryTest.java:234)
model2异常信息
java.lang.ArithmeticException: / by zero
  at org.ymx.test0810.TryTest.model2HaveException(TryTest.java:81)
  at org.ymx.test0810.TryTest.main(TryTest.java:239)
model3异常信息
java.lang.ArithmeticException: / by zero
  at org.ymx.test0810.TryTest.model3HaveException(TryTest.java:115)
  at org.ymx.test0810.TryTest.main(TryTest.java:244)
model4异常信息
java.lang.ArithmeticException: / by zero
  at org.ymx.test0810.TryTest.model4HaveException(TryTest.java:150)
  at org.ymx.test0810.TryTest.main(TryTest.java:249)
model5异常信息
java.lang.ArithmeticException: / by zero
  at org.ymx.test0810.TryTest.model5HaveException(TryTest.java:184)
  at org.ymx.test0810.TryTest.main(TryTest.java:254)
model6异常信息
java.lang.ArithmeticException: / by zero
  at org.ymx.test0810.TryTest.model6HaveException(TryTest.java:218)
  at org.ymx.test0810.TryTest.main(TryTest.java:259)
Process finished with exit code 0

结论:

1、不管有没有出现异常,finally块中代码都会执行;

2、当try和catch中有return时,finally仍然会执行;

3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的;

4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。

任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally存在的话。如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,编译器把finally中的return实现为一个warning。


相关文章
每日一道面试题之try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗?
每日一道面试题之try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗?
173 0
|
2月前
哪些情况可能会导致 try-catch 语句不捕获异常?
【10月更文挑战第12天】在实际应用中,可能还会存在其他一些情况导致异常不被捕获。因此,在使用`try-catch`语句时,需要仔细考虑各种可能的情况,以确保异常能够被正确地捕获和处理。
402 1
try catch finally,try 里面有 return,finally 还执行吗?
try catch finally,try 里面有 return,finally 还执行吗?
79 0
16 # 实现 catch 方法
16 # 实现 catch 方法
60 0
|
存储 IDE Java
try catch finally 执行顺序总结
try catch finally 执行顺序总结
133 0
|
编译器
try{...}catch(){...}finally{...}语句你真的理解吗?
try{...}catch(){...}finally{...}语句你真的理解吗?
|
Web App开发 JavaScript 前端开发
啊,似乎没有真正理解 try...catch...finally!
啊,似乎没有真正理解 try...catch...finally!
495 0
啊,似乎没有真正理解 try...catch...finally!
|
Java 程序员 编译器
技术大佬:我去,你竟然还在用 try–catch-finally
技术大佬:我去,你竟然还在用 try–catch-finally
128 0
技术大佬:我去,你竟然还在用 try–catch-finally
try-catch-finally 语句执行笔记
try-catch-finally 语句执行笔记
130 0