avatar

异常处理准则

异常中断程序问题

1
一旦出现,会终止程序

常见的能够终止程序的异常

一旦触发此类异常,程序将就此终止

NullPointerException: 空指针异常

异常信息

1
2
3
Exception in thread "main" java.lang.NullPointerException
at com.itpm.project.ExceptionHandlingTest.nullPointerException(ExceptionHandlingTest.java:22)
at com.itpm.project.ExceptionHandlingTest.main(ExceptionHandlingTest.java:8)

ArithmeticException: 算数异常

异常信息

1
2
3
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.itpm.project.ExceptionHandlingTest.ArithmeticException(ExceptionHandlingTest.java:46)
at com.itpm.project.ExceptionHandlingTest.main(ExceptionHandlingTest.java:11)

RuntimeException: 运行时异常

异常信息

1
2
3
Exception in thread "main" java.lang.RuntimeException
at com.itpm.project.ExceptionHandlingTest.runtimeExceptionTest(ExceptionHandlingTest.java:40)
at com.itpm.project.ExceptionHandlingTest.main(ExceptionHandlingTest.java:14)

异常处理的准则

1
2
3
4
1.释放资源必须放置到finally从句中
2.尽可能地缩小try...catch语句的监控范围
3.优先使用专用的异常类处理异常,最后再使用Exception异常来处理
4.在catch从句中,尽可能地恢复异常或详细地输出异常信息

finally从句的特性

1
2
1.无论程序运行是否发生异常,发生任何异常,finally从句中的代码都会被执行,即便在trycatch中使用return也不会影响finally从句中的代码的执行;除非是trycatch中使用了System.exit
2.在finally从句中,可以存放回收资源的代码,如释放数据库连接,释放IO对象,清空集合,设置大对象为null,以减少大对象上的强引用,从而提升大对象的回收时间

代码展示

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
public class ExceptionHandlingTest {

public static void main(String[] args) {

/* 无异常正常运行方法 */
noAbnormalities();

System.out.println("-----------------");

/* 有异常正常运行方法 */
thereIsAbnormal();

System.out.println("-----------------");

/* 有return的无异常正常运行方法 */
haveRETURNOfNoAbnormalities();

System.out.println("-----------------");

/* 有return的存在异常正常运行方法 */
haveRETURNOfThereIsAbnormal();

System.out.println("-----------------");

/* 执行System.exit的无异常方法 */
useSystemExitNoAbnormalities();

System.out.println("-----------------");

/* 执行System.exit的无异常方法 */
useSystemExitThereIsAbnormal();

System.out.println("-----------------");

}

public static void noAbnormalities() {

try {
System.out.println("代码无异常 try 已运行");
} catch (Exception e) {
System.out.println("代码存在异常 catch 已运行");
e.printStackTrace();
} finally {
System.out.println("finally 已运行");
}
}

public static void thereIsAbnormal() {

try {
int i = 1 / 0;
System.out.println("代码无异常 try 已运行");
} catch (Exception e) {
System.out.println("代码存在异常 catch 已运行");
e.printStackTrace();
} finally {
System.out.println("finally 已运行");
}
}

public static void haveRETURNOfNoAbnormalities() {

try {
System.out.println("代码无异常 try 已运行 代码块中return将运行");
return;
} catch (Exception e) {
System.out.println("catch 已运行");
e.printStackTrace();
return;
} finally {
System.out.println("return运行结束 finally 已运行");
}
}

public static void haveRETURNOfThereIsAbnormal() {

try {
int i = 1 / 0;
System.out.println("try 已运行");
return;
} catch (Exception e) {
System.out.println("代码存在异常 catch 已运行 代码块中return将运行");
e.printStackTrace();
return;
} finally {
System.out.println("return运行结束 finally 已运行");
}
}

public static void useSystemExitNoAbnormalities() {

try {
System.out.println("try 已运行, System.exit方法将执行");
/* 正常结束当前正在运行的Java虚拟机 */
System.exit(0);
} catch (Exception e) {
System.out.println("代码存在异常 catch 已运行 System.exit方法将执行");
e.printStackTrace();
/* 正常结束当前正在运行的Java虚拟机 */
System.exit(0);
} finally {
System.out.println("System.exit方法已执行 finally 已运行");
}
}

public static void useSystemExitThereIsAbnormal() {

try {
int i = 1 / 0;
System.out.println("try 已运行, System.exit方法将执行");
/* 正常结束当前正在运行的Java虚拟机 */
System.exit(0);
} catch (Exception e) {
System.out.println("代码存在异常 catch 已运行 System.exit方法将执行");
e.printStackTrace();
/* 正常结束当前正在运行的Java虚拟机 */
System.exit(0);
} finally {
System.out.println("System.exit方法已执行 finally 已运行");
}
}
}

打印展示

异常处理

文章作者: 123
文章链接: https://gao5805123.github.io/123/2021/05/17/%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E5%87%86%E5%88%99/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 123
打赏
  • 微信
    微信
  • 支付宝
    支付宝