2里依然有这个问题,下面是E文的thinking in java2中相对应的一段: Pitfall: the lost exception In general, Java’s exception implementation is quite outstanding, but unfortunately there’s a flaw. Although exceptions are an indication of a crisis in your program and should never be ignored, it’s possible for an exception to simply be lost. This happens with a particular configuration using a finally clause: //: c10:LostMessage.java // How an exception can be lost.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 { LostMessage lm = new LostMessage(); try { lm.f(); } finally { lm.dispose(); } } } ///:~ The output is: Exception in thread "main" A trivial exception at LostMessage.dispose(LostMessage.java:21) at LostMessage.main(LostMessage.java:29) You can see that there’s no evidence of the VeryImportantException, which is simply replaced by the HoHumException in the finally clause. This is a rather serious pitfall, since it means that an exception can be completely lost, and in a far more subtle and difficult-to-detect fashion than the example above. In contrast, C++ treats the situation in which a second exception is thrown before the first one is handled as a dire programming error. Perhaps a future version of Java will repair this problem (on the other hand, you will typically wrap any method that throws an exception, such as dispose( ), inside a try-catch clause). [ Add Comment ]
try is used to do what you want to do. catch is used to handle the exception, one try block can be followed by several cathch block, but at most one finally block.
我在JDK1.3下试了,确实是这样: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 { LostMessage lm = new LostMessage(); try { lm.f(); } catch(Exception e){ System.out.println("in catch..."); lm.dispose(); } finally { System.out.println("in finally..."); //lm.dispose(); } } }执行结果是: lm.f()掷出异常-> 进入finally -> System.out.println("in finally...") -> lm.dispose()掷出异常 -> 这时由于已经没有try/catch了,所以直接 由main掷出这个异常,进入系统(第一个异常没有被掷出)好像有一种Java中关于异常处理的设计Pattern和这个相似,可以用来做 “异常的转换”,即将一个异常转换为另一个异常,然后掷出转换后的异常。
Pitfall: the lost exception
In general, Java’s exception implementation is quite outstanding, but unfortunately there’s a flaw. Although exceptions are an indication of a crisis in your program and should never be ignored, it’s possible for an exception to simply be lost. This happens with a particular configuration using a finally clause:
//: c10:LostMessage.java
// How an exception can be lost.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 {
LostMessage lm = new LostMessage();
try {
lm.f();
} finally {
lm.dispose();
}
}
} ///:~
The output is:
Exception in thread "main" A trivial exception
at LostMessage.dispose(LostMessage.java:21)
at LostMessage.main(LostMessage.java:29)
You can see that there’s no evidence of the VeryImportantException, which is simply replaced by the HoHumException in the finally clause. This is a rather serious pitfall, since it means that an exception can be completely lost, and in a far more subtle and difficult-to-detect fashion than the example above. In contrast, C++ treats the situation in which a second exception is thrown before the first one is handled as a dire programming error. Perhaps a future version of Java will repair this problem (on the other hand, you will typically wrap any method that throws an exception, such as dispose( ), inside a try-catch clause). [ Add Comment ]
这说的是,在处理一个异常的过程中又产生了新的异常,使得旧的异常信息丢失。
其实对于一个程序而言,任何异常都应该解决,不然都会影响正常运行。
从这个思路出发,这也许就不是什么问题啦。
当然,最好是不要在finally{}中放可能产生异常的代码。
同一时间没理由产生两个异常的,只能一个一个来,可能记录异常的那样东西在JVM只有一个,所以只好由新产生的一个冲掉前面的。只用try不用catch本身就是不正当的使用手法还有他说C++如何如何,C++里面根本没有finally这个词,不可能产生这种情况,而且强制catch,如果没有catch到的话,程序会中断,我看也没有强到哪去嘛
catch is used to handle the exception,
one try block can be followed by several cathch block, but at most one finally block.
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 {
LostMessage lm = new LostMessage();
try {
lm.f();
}
catch(Exception e){
System.out.println("in catch...");
lm.dispose();
}
finally {
System.out.println("in finally...");
//lm.dispose();
}
}
}执行结果是:
lm.f()掷出异常->
进入finally ->
System.out.println("in finally...") ->
lm.dispose()掷出异常 -> 这时由于已经没有try/catch了,所以直接
由main掷出这个异常,进入系统(第一个异常没有被掷出)好像有一种Java中关于异常处理的设计Pattern和这个相似,可以用来做
“异常的转换”,即将一个异常转换为另一个异常,然后掷出转换后的异常。