-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.java.exception.xml
More file actions
166 lines (151 loc) · 4.63 KB
/
section.java.exception.xml
File metadata and controls
166 lines (151 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="index"><?dbhtml dir="throw" ?>
<title>异常处理</title>
<section>
<title>抛出异常</title>
<programlisting>
<![CDATA[
throw new NullPointerException();
throw new IOException();
throw new FileNotFoundException("文件不存在");
throw new ArrayIndexOutOfBoundsException("数字范围越界");
]]>
</programlisting>
</section>
<section id="try-with-resources">
<title>try-with-resources</title>
<para>打开的系统资源,比如流、文件或者 Socket 连接等,都需要被开发者手动关闭资源,否则将会造成资源占用和泄露。</para>
<programlisting>
<![CDATA[
package cn.netkiller.demo;
import java.io.*;
class ExceptionTest {
public static void main(String[] args) {
BufferedReader br = null;
String line;
try {
System.out.println("Entering try block");
br = new BufferedReader(new FileReader("test.txt"));
while ((line = br.readLine()) != null) {
System.out.println("Line =>"+line);
}
} catch (IOException e) {
System.out.println("IOException in try block =>" + e.getMessage());
} finally {
System.out.println("Entering finally block");
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
System.out.println("IOException in finally block =>"+e.getMessage());
}
}
}
}
]]>
</programlisting>
<para>try-with-resources 语句关闭所有实现 AutoCloseable 接口的资源。</para>
<programlisting>
<![CDATA[
package cn.netkiller.demo;
import java.io.*;
public class ExceptionTest {
public static void main(String[] args) {
String line;
try(BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
while ((line = br.readLine()) != null) {
System.out.println("Line =>"+line);
}
} catch (IOException e) {
System.out.println("IOException in try block =>" + e.getMessage());
}
}
}
]]>
</programlisting>
<para>无需再使用 close() 关闭资源 try 执行完毕后会自动释放资源。</para>
</section>
<section id="SneakyThrows">
<title>SneakyThrows</title>
<section>
<title>处理所有异常 Exception</title>
<programlisting>
<![CDATA[
public static void exceptionTest() {
File file = new File("/tmp/test.txt");
try {
FileReader fileReader = new FileReader(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void exceptionTest() throws Exception {
File file = new File("/tmp/test.txt");
FileReader fileReader = new FileReader(file);
}
]]>
</programlisting>
<para>使用 @SneakyThrows 注解,替代写法</para>
<programlisting>
<![CDATA[
@SneakyThrows
public static void exceptionTest(String pathname) {
File file = new File(pathname);
FileReader fileReader = new FileReader(file);
}
]]>
</programlisting>
</section>
<section>
<title>处理特定异常</title>
<programlisting>
<![CDATA[
public static void exceptionTest() {
File file = new File("/tmp/test.txt");
try {
FileReader fileReader = new FileReader(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public static void exceptionTest() throws FileNotFoundException {
File file = new File("/tmp/test.txt");
FileReader fileReader = new FileReader(file);
}
]]>
</programlisting>
<para>使用 @SneakyThrows 注解,替代写法</para>
<programlisting>
<![CDATA[
@SneakyThrows(FileNotFoundException.class)
public static void exceptionTest(File file) {
FileReader fileReader = new FileReader(file);
}
]]>
</programlisting>
</section>
<section>
<title>抛出异常</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import lombok.SneakyThrows;
public class Test {
@SneakyThrows
public static void throwExceptionTest(String message) {
throw new IllegalStateException(message);
}
public static void main(String[] args) {
try {
throwExceptionTest("Test");
} catch (Exception e) {
System.out.println(e);
}
}
}
]]>
</programlisting>
</section>
</section>
</chapter>