https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3

Optionally replace a try statement with the desugaring of a try-with-resources statement.  The canonical desugaring of
 try ResourceSpecification
   Block
is
 {
   final VariableModifiers_minus_final R #resource = Expression;
   Throwable #primaryException = null;

   try ResourceSpecificationtail
     Block
   catch (Throwable #t) {
     #primaryException = t;
     throw #t;
   } finally {
     if (#resource != null) {
       if (#primaryException != null) {
         try {
           #resource.close();
         } catch(Throwable #suppressedException) {
           #primaryException.addSuppressed(#suppressedException);
         }
       } else {
         #resource.close();
       }
     }
   }

举个解语法糖的例子,如下:

public class TryCatchFinally {
	public static void main(String[] args) {
		try (BufferedReader br = new BufferedReader(new FileReader("AutoCloseTest.java"));
				PrintStream ps = new PrintStream(new FileOutputStream("readme.txt"))) {
			System.out.println(br.readLine());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			System.out.println("default");
		}
	}
}  

调用Lower类的visitTry()方法,传递的参数Tree如下截图所示。

final BufferedReader br = new BufferedReader(new FileReader("AutoCloseTest.java"));
/*synthetic*/ Throwable primaryException0$ = null;
try {
    final PrintStream ps = new PrintStream(new FileOutputStream("readme.txt"));
    /*synthetic*/ Throwable primaryException1$ = null;
    try {
        System.out.println(br.readLine());
    } catch (/*synthetic*/ final Throwable t$) {
        primaryException1$ = t$;
        throw t$;
    } finally {
        if (ps != null)
        	if (primaryException1$ != null) try {
                    ps.close();
	        } catch (Throwable x2) {
	            primaryException1$.addSuppressed(x2);
	        }
        else ps.close();
    }
} catch (/*synthetic*/ final Throwable t$) {
    primaryException0$ = t$;
    throw t$;
} finally {
    if (br != null)
    	if (primaryException0$ != null) try {
                br.close();
	    } catch (Throwable x2) {
	        primaryException0$.addSuppressed(x2);
	    }
    else br.close();  

由如上的代码可知,如果try块产生了异常,则会忽略close产生的异常(如果真的产生异常的话);否则才会抛出close产生的异常(如果真的产生异常的话)。

  

Javac语法糖之TryCatchFinally的更多相关文章

  1. Javac语法糖之内部类

    在Javac中解语法糖主要是Lower类来完成,调用这个类的入口函数translateTopLevelClass即可.这个方法只是JavacCompiler类的desugar方法中进行了调用. 首先来 ...

  2. Javac语法糖之EnumSwitch

    在Switch中可以使用的类型有枚举.字符串类型与整形int类型,下面来具体看这几个类型. 1.switch为枚举类型 枚举类: enum Fruit { APPLE,ORINGE } 调用javac ...

  3. Javac语法糖之增强for循环

    加强的for循环有两种,遍历数组和实现了Iterable接口的容器.javac通过visitForeachLoop()方法来实现解语法糖,代码如下: /** Translate away the fo ...

  4. Javac语法糖之其它

    1.变长参数 class VarialbeArgumentsDemo { public static void doWork(int... a) {//可变参数 } public static voi ...

  5. Javac语法糖之Enum类

    枚举类在Javac中是被当作类来看待的. An enum type is implicitly final unless it contains at least one enum constant ...

  6. JVM(二):Java中的语法糖

    JVM(二):Java中的语法糖 上文讲到在语义分析中会对Java中的语法糖进行解糖操作,因此本文就主要讲述一下Java中有哪些语法糖,每个语法糖在解糖过后的原始代码,以及这些语法糖背后的逻辑. 语法 ...

  7. Java中的10颗语法糖

    语法糖(Syntactic Sugar):也称糖衣语法,指在计算机语言中添加的某种语法,这种语法对语言的功能没有影响,但是更方便程序员使用.通常来说,使用语法糖能够增加程序的可读性,减少程序代码出错的 ...

  8. 转:【深入Java虚拟机】之六:Java语法糖

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/18011009 语法糖(Syntactic Sugar),也称糖衣语法,是由英国计算机学家P ...

  9. 第十三节:实际开发中使用最多的监视锁Monitor、lock语法糖的扩展、混合锁的使用(ManualResetEvent、SemaphoreSlim、ReaderWriterLockSlim)

    一. 监视锁(Monitor和lock) 1. Monitor类,限定线程个数的一把锁,两个核心方法: Enter:锁住某个资源. Exit:退出某一个资源. 测试案例:开启5个线程同时对一个变量进行 ...

随机推荐

  1. (深搜)Sum It Up -- poj --1564

    链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

  2. 机器学习---K最近邻(k-Nearest Neighbour,KNN)分类算法

    K最近邻(k-Nearest Neighbour,KNN)分类算法 1.K最近邻(k-Nearest Neighbour,KNN) K最近邻(k-Nearest Neighbour,KNN)分类算法, ...

  3. hdu 5062 单峰数(12321)的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=5062 模拟筛出对称单峰数(12321)的个数,水题 #include <cstdio> #inclu ...

  4. web问题

    模拟form提交过程中form(hidden)时:The frame requesting access has a protocol of "http", the frame b ...

  5. 让Easy UI 的DataGrid直接内嵌的JSON对象,并重写form load 方法

    前言 我有这样的JSON对象 { "UserName": "jf", "UserPwd": "123456", &quo ...

  6. linux系统编程之文件与IO(一):文件描述符、open,close

    什么是IO? 输入/输出是主存和外部设备之间拷贝数据的过程 设备->内存(输入操作) 内存->设备(输出操作) 高级I/O ANSI C提供的标准I/O库称为高级I/O,通常也称为带缓冲的 ...

  7. winform之combobox

    绑定键值对: ArrayList mylist = new ArrayList(); mylist.Add(new DictionaryEntry("WinFormURL", &q ...

  8. C# 标准事件模式

    .NET框架为事件定义了一个标准模式,它的目的是保持框架和用户代码之间的一致性. 标准事件的模式核心是SystemEventArgs——预定义的没有成员的框架类(不同于静态Empty属性) Event ...

  9. C# 委托和接口

    能用委托解决的事情,接口也都可以解决.如下所示: public static void Main() { , , , }; Util.TransformAll(values, new Squarer( ...

  10. NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json

    一.简介 1.读取配置文件是开发过程中使用非常频繁的操作.属称”不能写死“ 二.NetCore读取配置文件 1.新建一个静态公共变量,属称单例. 2.在程序Startup启动时,将系统变量传递给单例. ...