java 丢失的异常

采用finally从句中的,可能会丢失异常

package thinking;
//: 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();
    }
}

} ///:~
  

VeryImportantException 被finally从句中的HoHumException的异常替代了,即出错信息丢失了。

猜你喜欢

转载自www.cnblogs.com/newlangwen/p/9191454.html