java7新特性之Try-with-resources (TWR)


This change is easy to explain, but it has proved to have hidden subtleties, which

made it much less easy to implement than originally hoped. The basic idea is to allow

a resource (for example, a file or something a bit like one) to be scoped to a block in

such a way that the resource is automatically closed when control exits the block.

This is an important change, for the simple reason that virtually no one gets manual resource closing 100 percent right. Until recently, even the reference how-tos from Sun were wrong. The proposal submitted to Project Coin for this change

includes the astounding claim that two-thirds of the uses of close() in the JDK had

bugs in them!

Fortunately, compilers can be made to produce exactly the sort of pedantic, boilerplate code that humans so often get wrong, and that’s the approach taken by this change.

This is a big help in writing error-free code. To see just how helpful, consider how

you’d write a block of code that reads from a stream coming from a URL (url) and

writes to a file (out) with Java 6. Here’s one possible solution.




How close did you get? The key point here is that when handling external resources,

Murphy’s Law applies—anything can go wrong at any time:

■ The InputStream can fail to open from the URL, to read from it, or to close

properly.

■ The File corresponding to the OutputStream can fail to open, to write to it, or

to close properly.

■ A problem can arise from some combination of more than one factor.

This last possibility is where a lot of the headaches come from—a combination of

exceptions is very difficult to deal with well.

This is the main reason for preferring the new syntax—it’s much less error-prone.

The compiler isn’t susceptible to the mistakes that every developer will make when trying to write this type of code manually.



Let’s look at the Java 7 code for performing the same task as listing 1.3. As before, url

is a URL object that points at the entity you want to download, and file is a File object

where you want to save what you’re downloading. Here’s what this looks like in Java 7.
try (OutputStream out = new FileOutputStream(file);
InputStream is = url.openStream() ) {
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
}

This basic form shows the new syntax for a block with automatic management—the

try with the resource in round brackets. For C# programmers, this is probably a bit

reminiscent of a using clause, and that’s a good conceptual starting point when working with this new feature. The resources are used by the block, and they’re automatically disposed of when you’re done with them.

You still have to be careful with try-with-resources, as there are cases where a

resource might still not be closed.
For example, the following code would not close its

FileInputStream properly if there was an error creating the ObjectInputStream

from the file (someFile.bin).

try ( ObjectInputStream in = new ObjectInputStream(new
FileInputStream("someFile.bin")) ) {
...
}


Let’s assume that the file (someFile.bin) exists, but it might not be an ObjectInput

file, so the file might not open correctly. Therefore, the ObjectInputStream wouldn’t

be constructed and the FileInputStream wouldn’t be closed!

The correct way to ensure that try-with-resources always works for you is to split the

resources into separate variables.

try ( FileInputStream fin = new FileInputStream("someFile.bin");
ObjectInputStream in = new ObjectInputStream(fin) ) {
...
}

One other aspect of TWR is the appearance of enhanced stack traces and suppressed

exceptions. Prior to Java 7, exception information could be swallowed when handling

resources
. This possibility also exists with TWR, so the stack traces have been enhanced

to allow you to see the type information of exceptions that would otherwise be lost.

For example, consider this snippet, in which a null InputStream is returned from

a method:

try(InputStream i = getNullStream()) {
i.available();
}

This will give rise to an enhanced stack trace, in which the suppressed NullPointerException (NPE for short) can be seen:


Exception in thread "main" java.lang.NullPointerException

at wgjd.ch01.ScratchSuprExcep.run(ScratchSuprExcep.java:23)

at wgjd.ch01.ScratchSuprExcep.main(ScratchSuprExcep.java:39)

Suppressed: java.lang.NullPointerException

at wgjd.ch01.ScratchSuprExcep.run(ScratchSuprExcep.java:24)

1 more





We encourage you to use try-with-resources as soon as you’re able, to eliminate unnecessary bugs from your codebase.



TWR and AutoCloseable

Under the hood, the TWR feature is achieved by the introduction of a new interface,

called AutoCloseable, which a class must implement in order to be able to appear

as a resource in the new TWR try clause. Many of the Java 7 platform classes have

been converted to implement AutoCloseable (and it has been made a superinterface of Closeable), but you should be aware that not every aspect of the platform

has yet adopted this new technology. It’s included as part of JDBC 4.1, though.

For your own code, you should definitely use TWR whenever you need to work with

resources. It will help you avoid bugs in your exception handling.


读书笔记:The
Well-Grounded Java Develope


java7新特性之Try-with-resources (TWR)的更多相关文章

  1. java7新特性之Diamond syntax

    java7新特性之Diamond syntax Java 7 also introduces a change that means less typing for you when dealing ...

  2. java7新特性 java8新特性

    Java 7 的7个新特性 Java7语法新特性 JAVA8 十大新特性详解 http://www.jb51.net/article/48304.htm

  3. Java7 新特性 数值文本表示法

    今天和大家分享下 java7中新特性-数值文本表示法 首先,在原来jdk1.6中 如果需要将一个二进制的数值转换成十进制的话,一般情况下都会以下面的代码方式去实现. public static voi ...

  4. java7 新特性 总结版

    Java7语法新特性: 前言,这是大部分的特性,但还有一些没有写进去,比如多核 并行计算的支持加强 fork join 框架:这方面并没有真正写过和了解.也就不写进来了. 1. switch中增加对S ...

  5. Java7新特性

    ① 新增了switch对字符串的支持,也就是说可以在switch之后直接使用字符串来进行判断,语法基本与Java7之前支持的语法一样. ② 对数值字面量的增强支持,首先是可以在源代码中直接使用二进制数 ...

  6. Java7 新特性: try-with-resources

    Try-with-resources是java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源. 利用Try-Catch-Finally管理资源(旧的代码风格)在ja ...

  7. Java7 新特性 —— java.nio.file 文件操作

    本文部分摘自 On Java 8 自 Java7 开始,Java 终于简化了文件读写的基本操作,新增了 java.nio.file 库,通过与 Java8 新增的 stream 结合可以使得文件操作变 ...

  8. Java7 新特性 switch 可以使用String

    今天和大家分享下 在java7中可以使用String 作为switch 中的参数. 原来在java7之前,switch只能去接收一个 byte.char.short.int 类型 现在在java7中 ...

  9. Java7新特性(一)Coin

    1.语法糖 数字下划线   2.switch语句中的String   3.multicatch   4.final重抛 对比上份代码   5.try-with-resources(TWR) AutoC ...

随机推荐

  1. day25-2 OSI协议和socket抽象层

    目录 OSI协议 物理层 数据链路层 以太网协议 Mac地址 广播地址 网络层 获取对方Mac地址(ARP协议) 传输层 TCP协议 UDP协议 应用层 socket抽象层 OSI协议 互联网的本质就 ...

  2. react-native IOS Build input file cannot be found: '~~~~~/node_modules/react-native/Libraries/WebSocket/libfishhook.a'

    尝试添加或者删除libfishhook.a 如果解决了你的问题请点个赞!

  3. 关于C/C++的一些思考(3)

    操作符重载函数(Operator Overload Function)的基本概念: 目的是以与对待内置数据类型相同的方式对待用户自定义类型(程序执行速度会受到影响),限制是不能随意选择函数名和参数个数 ...

  4. Ubuntu环境修改IP地址方法

    ubuntu环境修改IP地址方法和CentOS系统修改方法不太一样.ubuntu系统修改IP地址方法如下: 编辑/etc/network/interfaces,增加以下内容: auto eth0 if ...

  5. mysql cluster配置

    依赖包要求:cmake     gcc    gcc-c++     ncurses     Perl     ncurses-devel 在7.3以及更高的版本中, WITH_NDB_JAVA是默认 ...

  6. [Python3网络爬虫开发实战] 7-动态渲染页面爬取

    在前一章中,我们了解了Ajax的分析和抓取方式,这其实也是JavaScript动态渲染的页面的一种情形,通过直接分析Ajax,我们仍然可以借助requests或urllib来实现数据爬取. 不过Jav ...

  7. MySQL简单查询和单表查询

    MySQL记录操作 概览 MySQL数据操作: DML 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用 ...

  8. STM32——GPIO口的八种工作模式

    GPIO的输入工作模式1——输入浮空模式: GPIO_Mode_IN_FLOATING =0x04 工作原理:配置完相应寄存器为此工作模式后,高低电平信号通过1处的IO口输入进去,由于寄存器配置了的缘 ...

  9. pcb中几个层的解释

    阻焊层(Solder Mask):又称为绿油层,是PCB的非布线层,用于制成丝网漏印板,将不需要焊接的地方涂一层阻焊物质,防止焊接PCB时焊锡在高温下的流动性.在阻焊层上预留的焊盘大小,要比实际焊盘大 ...

  10. 【04】emmet系列之编辑器

     [01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 前端开发人员,常用的是s ...