一.java的异常实现也是又缺陷的,异常作为程序出错的标志决不能被忽略,但它还是可能被轻易地忽略.下了可以看到前一个异常还没处理就抛出下一个异常,没有catch捕获异常,它被finally抛出下一个异常所取代
//: exceptions/LostMessage.java
// How an exception can be lost.
package object;
class VeryImportantException extends Exception {
public String toString() {
return "A very important exception!";
}
} class HoHumException extends Exception {
public String toString() {
return "A trivial exception";
}
} public class LostMessage {
void f() throws VeryImportantException {
throw new VeryImportantException();
}
void dispose() throws HoHumException {
throw new HoHumException();
}
public static void main(String[] args) {
try {
LostMessage lm = new LostMessage();
try {
lm.f();
} finally {
lm.dispose();
}
} catch(Exception e) {
System.out.println(e);
}
}
} /* Output:
A trivial exception
*///:~
二.在finally中加入return语句,没有用catch语句捕获异常,下面这种情况编译器会报警:finally块未正常完成
package exceptions;
//: exceptions/ExceptionSilencer.java public class ExceptionSilencer {
public static void main(String[] args) {
try {
throw new RuntimeException();
} finally {
// Using 'return' inside the finally block
// will silence any thrown exception.
return;
}
}
} ///:~