-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionTest.java
More file actions
77 lines (67 loc) · 2.07 KB
/
ExceptionTest.java
File metadata and controls
77 lines (67 loc) · 2.07 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
package Chapter11Throwable;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class ExceptionTest implements Closeable{
public void test() throws FileNotFoundException{
//String path= this.getClass().getClassLoader().getResource("/").getPath();
//System.out.println("��·��::"+path);
//����Դ��try��� ����try-with-resource �����Զ�������Դ���á���������
try(Scanner sc=new Scanner(new FileInputStream("readme.txt"))){
while(sc.hasNext()){
System.out.println(sc.next());
}
}
}
//��ջ����
public int factorial(int a){
System.out.println("factorial::"+a);
Throwable t=new Throwable();
StackTraceElement[] frans=t.getStackTrace();
for(StackTraceElement f:frans){
System.out.println(f);
}
int s;
if(a<=1) s=a;
else s=a*factorial(a-1);
System.out.println("return ::"+s);
return s;
}
//��catch���ٴ��׳��쳣���쳣��
public void ExceptionAgain() throws Throwable{
try {
Scanner sc=new Scanner(new FileInputStream("/readme.txt"));
} catch (FileNotFoundException e) {
Throwable t=new Throwable("Scanner error");
t.initCause(t);
throw t;
}
}
public static void main(String[] args) {
ExceptionTest test= new ExceptionTest();
try {
test.test();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(test.factorial(3));
/*try {
test.ExceptionAgain();
} catch (Throwable e) {
// TODO Auto-generated catch block
//�ڴ˴������쳣��Դ
//�쳣ʹ�û�������ֻ���쳣�����ʹ���쳣
e.getCause();
System.out.println("�쳣��Դ����"+e.getCause());
}*/
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
System.out.println("���ùر���Դ�ķ���");
}
}