File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -938,11 +938,49 @@ if(t == null)
938938
939939RuntimeException 代表的是编程错误:
940940
941- 1. 无法预料的错误。比如从你控制范围之外传递进来的mull引用 。
941+ 1. 无法预料的错误。比如从你控制范围之外传递进来的 null 引用 。
9429422. 作为程序员,应该在代码中进行检查的错误。(比如对于ArrayIndexOutOfBoundsException ,就得注意一下数组的大小了。)在一个地方发生的异常,常常会在另一个地方导致错误。
943943
944944在这些情况下使用异常很有好处,它们能给调试带来便利。
945945
946+ 如果不捕获这种类型的异常会发生什么事呢?因为编译器没有在这个问题上对异常说明进行强制检查,RuntimeException类型的异常也许会穿越所有的执行路径直达main ()方法,而不会被捕获。要明白到底发生了什么,可以试试下面的例子:
947+
948+ ```java
949+ // exceptions/NeverCaught.java
950+ // Ignoring RuntimeExceptions
951+ // {ThrowsException}
952+ public class NeverCaught {
953+ static void f() {
954+ throw new RuntimeException (" From f()" );
955+ }
956+ static void g() {
957+ f();
958+ }
959+ public static void main(String [] args) {
960+ g();
961+ }
962+ }
963+ ```
964+
965+ 输出结果为:
966+
967+ ```java
968+ ___[ Error Output ]___
969+ Exception in thread " main" java.lang. RuntimeException :
970+ From f()
971+ at NeverCaught . f(NeverCaught . java: 7 )
972+ at NeverCaught . g(NeverCaught . java: 10 )
973+ at NeverCaught . main(NeverCaught . java: 13 )
974+ ```
975+
976+ 如果RuntimeException 没有被捕获而直达main(),那么在程序退出前将调用异常的printStackTrace()方法。
977+
978+ 你会发现,RuntimeException (或任何从它继承的异常)是一个特例。对于这种异常类型,编译器不需要异常说明,其输出被报告给了System . err。
979+
980+ 请务必记住:只能在代码中忽略RuntimeException (及其子类)类型的异常,因为所有受检查类型异常的处理都是由编译器强制实施的。
981+
982+ 值得注意的是:不应把Java 的异常处理机制当成是单一用途的工具。是的,它被设计用来处理一些烦人的运行时错误,这些错误往往是由代码控制能力之外的因素导致的;然而,它对于发现某些编译器无法检测到的编程错误,也是非常重要的。
983+
946984< ! -- Performing Cleanup with finally -- >
947985
948986## finally 关键字
You can’t perform that action at this time.
0 commit comments