Java的异常处理同样存在着瑕疵.(读Thinking in Java Found Edition有感)
实例一
package edu.dlut.zxf.test;
class VeryImportantException extends Exception {
public String toString() {
return "A very important exception!";
}
}
class AnotherException extends Exception {
public String toString() {
return "Another Exception";
}
}
public class LostMessageTest {
public void f() throws VeryImportantException {
throw new VeryImportantException();
}
public void g() throws AnotherException {
throw new AnotherException();
}
public static void main(String[] args) {
LostMessageTest test = new LostMessageTest();
try {
try {
test.f(); //抛弃的异常VeryImportantException没有被catch
} finally {
test.g();
}
} catch(Exception e) {
System.out.println(e.toString()); //此处catch的是AnotherException
}
}
}
/* Output:
Another Exception
*/
|
实例二
package edu.dlut.zxf.test;
public class LostMessageTest2 {
public static void main(String[] args) {
try {
throw new Exception();
} finally {
return;
}
}
}
|
阅读(1288) | 评论(0) | 转发(0) |