try语法糖

背景

临时接到一个查询数据库数据并生成txt文件,并打包zip上传到ftp的功能。
代码自然不算特别复杂,但是发现我们项目使用了1.7的jdk后,瞬间来了兴趣,以前一直想用没敢用的要拿上来试试看了。

简略代码如下

普通写法自然是类似这样

 1 private static void generateData(DateTime time, String dataPath) {
 2         Connection connection = (Connection)DBUtil.getConnection(DataBaseType.MySql, Constant.MYSQL_URL, Constant.MYSQL_USERNAME, Constant.MYSQL_PWD);
 3         try {
 4                ///查询语句
 5 
 6                /// 写入文件
 7 
 8         } catch (Exception e) {
 9 
10         }finally{
11                 IOUtils.closeQuiet(connection);
12         }
13     }

jdk提供的语法糖,类似C#的with关键字,写出代码如下:

 1 private static void generateData(DateTime time, String dataPath) {
 2         try (Connection connection = (Connection) DBUtil.getConnection(DataBaseType.MySql, Constant.MYSQL_URL,
 3                 Constant.MYSQL_USERNAME, Constant.MYSQL_PWD)) {
 4                ///查询语句
 5 
 6                /// 写入文件
 7 
 8         } catch (Exception e) {
 9 
10         }
11     }

可以看到,这里没有写finally,那么生效的语句是什么样子呢,我们用jad反编译下生成的class

 1 private static void generateData(DateTime time, String dataPath)
 2     {
 3         Connection connection;
 4         Throwable throwable;
 5         connection = (Connection)DBUtil.getConnection(DataBaseType.MySql, Constant.MYSQL_URL, Constant.MYSQL_USERNAME, Constant.MYSQL_PWD);
 6         throwable = null;
 7         try
 8         {
 9                ///查询语句
10 
11                /// 写入文件
12 
13         }
14         catch(Throwable throwable1)
15         {
16             throwable = throwable1;
17             throw throwable1;
18         }
19         if(connection != null)
20             if(throwable != null)
21                 try
22                 {
23                     connection.close();
24                 }
25                 catch(Throwable x2)
26                 {
27                     throwable.addSuppressed(x2);
28                 }
29             else
30                 connection.close();
31         break MISSING_BLOCK_LABEL_486;
32         Exception exception;
33         exception;
34         if(connection != null)
35             if(throwable != null)
36                 try
37                 {
38                     connection.close();
39                 }
40                 catch(Throwable x2)
41                 {
42                     throwable.addSuppressed(x2);
43                 }
44             else
45                 connection.close();
46         throw exception;
47         Exception e;
48         e;
49     }

jdk编译的时候已经很聪明地自动加上了finally还有关闭的语句了。
这样可以是我们更加专注于业务,不在写冗余的finally等代码了。

用法与注意点

采用此语法糖方式很简单,JDK≥1.7。
而且需要写在try()里面的变量支持JDK中提供的Autoclose接口

 1 //
 2 // Source code recreated from a .class file by IntelliJ IDEA
 3 // (powered by Fernflower decompiler)
 4 //
 5 
 6 package java.lang;
 7 
 8 public interface AutoCloseable {
 9     void close() throws Exception;
10 }

括号内语句支持多个变量,用分号分隔,类似就是多条语句

1 try(Connect conn = ....;
2 InputStream is = ......;)

这样,jdk在编译的时候就会自动生成相应的finally和close语句了。

 

posted on 2020-09-03 17:00  德邦总管  阅读(798)  评论(0编辑  收藏  举报

导航