虽然我们开始了Java8的旅程,但是很多人直接从java6上手了java8, 也许有一些JDK7的特性你还不知道,在本章节中带你回顾一下我们忘记了的那些特性。 尽管我们不能讲所有特性都讲一遍,挑出常用的核心特性拎出来一起学习。
异常改进
try-with-resources
这个特性是在JDK7种出现的,我们在之前操作一个流对象的时候大概是这样的:
1
2
3
4
5
6
7
8
9
10
11
12
|
try
{
// 使用流对象
stream.read();
stream.write();
}
catch
(Exception e){
// 处理异常
}
finally
{
// 关闭流资源
if
(stream !=
null
){
stream.close();
}
}
|
这样无疑有些繁琐,而且finally块还有可能抛出异常。在JDK7种提出了try-with-resources机制, 它规定你操作的类只要是实现了AutoCloseable接口就可以在try语句块退出的时候自动调用close 方法关闭流资源。
1
2
3
4
5
6
|
public
static
void
tryWithResources()
throws
IOException {
try
( InputStream ins =
new
FileInputStream(
"/home/biezhi/a.txt"
) ){
char
charStr = (
char
) ins.read();
System.out.print(charStr);
}
}
|
使用多个资源
1
2
3
4
5
6
|
try
( InputStream is =
new
FileInputStream(
"/home/biezhi/a.txt"
);
OutputStream os =
new
FileOutputStream(
"/home/biezhi/b.txt"
)
) {
char
charStr = (
char
) is.read();
os.write(charStr);
}
|
当然如果你使用的是非标准库的类也可以自定义AutoCloseable,只要实现其close方法即可。
捕获多个Exception
当我们在操作一个对象的时候,有时候它会抛出多个异常,像这样:
1
2
3
4
5
6
7
8
|
try
{
Thread.sleep(
20000
);
FileInputStream fis =
new
FileInputStream(
"/a/b.txt"
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
|
这样代码写起来要捕获很多异常,不是很优雅,JDK7种允许你捕获多个异常:
1
2
3
4
5
6
|
try
{
Thread.sleep(
20000
);
FileInputStream fis =
new
FileInputStream(
"/a/b.txt"
);
}
catch
(InterruptedException | IOException e) {
e.printStackTrace();
}
|
并且catch语句后面的异常参数是final的,不可以再修改/复制。
处理反射异常
使用过反射的同学可能知道我们有时候操作反射方法的时候会抛出很多不相关的检查异常,例如:
1
2
3
4
5
6
7
8
9
10
|
try
{
Class<?> clazz = Class.forName(
"com.biezhi.apple.User"
);
clazz.getMethods()[
0
].invoke(object);
}
catch
(IllegalAccessException e) {
e.printStackTrace();
}
catch
(InvocationTargetException e) {
e.printStackTrace();
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
|
尽管你可以使用catch多个异常的方法将上述异常都捕获,但这也让人感到痛苦。 JDK7修复了这个缺陷,引入了一个新类ReflectiveOperationException可以帮你捕获这些反射异常:
1
2
3
4
5
6
|
try
{
Class<?> clazz = Class.forName(
"com.biezhi.apple.User"
);
clazz.getMethods()[
0
].invoke(object);
}
catch
(ReflectiveOperationException e){
e.printStackTrace();
}
|