乐趣区

tryA-a-new-A-VS-try-finally

实现了 AutoCloseable 接口的类,可以在 try 的时候直接实例化对象。try 代码块完成之后,自动调用 close 方法,相当于在 finally 里主动调用。但是出现异常后的流程和 try finally 有什么不同呢?下面写代码测试一下。

首先定义一个类 Cat,实现 AutoCloseable 接口

class Cat implements AutoCloseable{void sayHello() throws Exception {Utils.println("calling sayHello(), I will throw an exception");
        throw new Exception("Exception in sayHello()");
    }

    @Override
    public void close() throws Exception {Utils.println("I'm closing, I will throw an exception");
        throw new Exception("Exception in close()");
    }
}

我们的这个 Cat 有以下特点:

  • sayHello 方法会抛出异常
  • close 方法也会抛出异常

test v1: ‘try(Cat cat = new Cat())’ VS ‘try finally’

没有 catch(不要被外层的 catch 迷惑,那只是为了打印异常)

static void testV1(){Utils.println("----try(Cat cat = new Cat())-----");
    try{try(Cat cat = new Cat()){cat.sayHello();
        }

    }catch (Exception e){Utils.println("cache error in main (" + e + "), let's see its stack trace");
        Utils.printStackTrace(e);
    }
    Utils.println("--------------");

    Utils.println("----try finally-----");
    try{
        Cat cat = null;
        try{cat = new Cat();
            cat.sayHello();}finally {if(cat != null){cat.close();
            }
        }
    }catch (Exception e){Utils.println("cache error in main (" + e + "), let's see its stack trace");
        Utils.printStackTrace(e);
    }
    Utils.println("--------------");
}

结果输出:

----test v1----------------------------------------
----try(Cat cat = new Cat())-----
calling sayHello(), I will throw an exception
I'm closing, I will throw an exception
cache error in main (java.lang.Exception: Exception in sayHello() ), let's see its stack trace
java.lang.Exception: Exception in sayHello() 
    at Cat.sayHello(Cat.java:4)
    at Test.testV1(Test.java:16)
    at Test.main(Test.java:4)
    Suppressed: java.lang.Exception: Exception in close()
        at Cat.close(Cat.java:10)
        at Test.testV1(Test.java:17)
        ... 1 more
--------------
----try finally-----
calling sayHello(), I will throw an exception
I'm closing, I will throw an exception
cache error in main (java.lang.Exception: Exception in close()), let's see its stack trace
java.lang.Exception: Exception in close()
    at Cat.close(Cat.java:10)
    at Test.testV1(Test.java:33)
    at Test.main(Test.java:4)
--------------

结论

  • try(Cat cat = new Cat())

    • try 代码块完成之后会自动调用 close
    • close 抛出的异常,被 Suppressed 了,外层捕获的只有 sayHello 的异常,但通过堆栈可以找到这个 Suppressed 的异常
  • try finally

    • 外层捕获的是在 finally 执行 close 时抛出的异常,sayHello 的异常完全不见了。

test v2: ‘try(Cat cat = new Cat()) catch{}’ VS ‘try catch finally’

有 catch,并且 catch 里再抛出异常

static void testV2(){Utils.println("----try(Cat cat = new Cat()) catch-----");
    try{try(Cat cat = new Cat()){cat.sayHello();
        } catch (Exception e) {Utils.println("cached err (" + e.getMessage() + "), I will throw an exception again");
            throw new Exception("Exception in catch", e);
        }

    }catch (Exception e){Utils.println("cache error in main (" + e + "), let's see its stack trace");
        Utils.printStackTrace(e);
    }
    Utils.println("-----------------------------------------");

    Utils.println("----try catch finally--------------------");
    try{
        Cat cat = null;
        try{cat = new Cat();
            cat.sayHello();} catch (Exception e) {Utils.println("cached err (" + e.getMessage() + "), I will throw an exception again");
            throw new Exception("Exception in catch", e);
        }finally {if(cat != null){cat.close();
            }
        }
    }catch (Exception e){Utils.println("cache error in main (" + e + "), let's see its stack trace");
        Utils.printStackTrace(e);
    }
    Utils.println("-------------------------------------------");
}

结果输出

----test v2------
----try(Cat cat = new Cat()){} catch{}-----
calling sayHello(), I will throw an exception
I'm closing, I will throw an exception
cached err (Exception in sayHello() ), I will throw an exception again
cache error in main (java.lang.Exception: Exception in catch), let's see its stack trace
java.lang.Exception: Exception in catch
    at Test.testV2(Test.java:50)
    at Test.main(Test.java:8)
-----------------------------------------
----try catch finally--------------------
calling sayHello(), I will throw an exception
cached err (Exception in sayHello() ), I will throw an exception again
I'm closing, I will throw an exception
cache error in main (java.lang.Exception: Exception in close()), let's see its stack trace
java.lang.Exception: Exception in close()
    at Cat.close(Cat.java:10)
    at Test.testV2(Test.java:70)
    at Test.main(Test.java:8)
-------------------------------------------
---------------------------------------------------------------------

结论

  • try(Cat cat = new Cat()) catch

    • catch 之前就调用了 close(符合 try 代码块完成之后会自动调用 close 这个结论)
    • catch 到的是 sayHello 的异常,close 抛出的异常依然被 Suppressed 了
    • catch 中再次抛出的异常被外层捕获
  • try catch finally

    • 先走 catch,再走 finally,所以 catch 捕获的是 sayHello 的异常
    • catch 中再次抛出的异常不见了,外层捕获的是在 finally 执行 close 时抛出的异常。

测试代码地址:https://github.com/kongxiangx…

退出移动版