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) throws Exception {
try{
LostMessage lm = new LostMessage();
try {
lm.f();
} finally {
lm.dispose();
}
}
catch(Exception e){
System.out.println(e);
}
}
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) throws Exception {
try{
LostMessage lm = new LostMessage();
try {
lm.f();
} finally {
lm.dispose();
}
}
catch(Exception e){
System.out.println(e);
}
}
} ///:~
这是Java编程思想这本书中的一个示例代码,程序的输出结果是 A trival exception
VertImportantException 不见了,作者将其描述为一个bug,当然这个是JDK1.5的版本,在1.7版本我试了一下同样的结果,仔细观察这段代码,在嵌套的第二个try语句中没有catch,作者可能理解的是我最后一个catch语句要将程序中没有catch到的语句全catch到,对此我不太苟同,我认为这不是个bug,异常对象产生了,需要catch才能将其捕获。