-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThrowsTest.java
More file actions
60 lines (54 loc) · 1.7 KB
/
Copy pathThrowsTest.java
File metadata and controls
60 lines (54 loc) · 1.7 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
package random.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
* try..catch..finally 会真正处理异常
* throws 只是将异常抛给了方法的调用者, 并没有把异常处理掉
*
* */
public class ThrowsTest {
public static void main(String[] args) {
try {
method2();
} catch (IOException e) {
e.printStackTrace();
System.out.println("errors catch!");
}
}
public static void method2() throws IOException {
method1();
}
public static void method1() throws FileNotFoundException, IOException {
// File file = new File("test.txt"); // not working, under project
File file = new File("Exception/test.txt"); // works!
System.out.println(file.getAbsoluteFile());
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while (data!= -1){
System.out.println((char) data);
data = fis.read();
}
fis.close();
System.out.println("hahaha");
}
// @Test
// public void test(){
// try {
// File file = new File("test.txt"); // works, under module
// FileInputStream fis = new FileInputStream(file);
// int data = fis.read();
// while (data!= -1){
// System.out.println((char) data);
// data = fis.read();
// }
// fis.close();
// System.out.println("hahaha");
// }catch (FileNotFoundException e){
// e.printStackTrace();
// }catch (IOException e){
// e.printStackTrace();
// }
// }
}