Java編程思想讀書筆記(10章中)
發(fā)表時間:2024-01-20 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]三. 以finally進行清理 1. 如果某段代碼不管是否發(fā)生異常都要執(zhí)行,那可把它改入finally塊中。 import java.sql.SQLException; class TestException public static void tSq...
三. 以finally進行清理
1. 如果某段代碼不管是否發(fā)生異常都要執(zhí)行,那可把它改入finally塊中。
import java.sql.SQLException;
class TestException{
public static void tSql() throws SQLException {
System.out.println("Originating the exception in tSql()");
throw new SQLException("throw in tSql");
}
public void f() throws SQLException{
try{
tSql();
}
catch(SQLException ex){
System.out.println("catch SQLException in f()");
throw ex;//(1)
}
finally{
System.out.println("finally in f()");
}
}
}
public class Test{
public static void main(String[] args){
TestException te = new TestException();
try{
te.f();
}
catch(SQLException ex){
System.out.println("catch te.f() SQLException in main");
}
catch(Exception ex){
System.out.println("catch te.f() Exception in main");
}
}
}
運行結(jié)果為:
Originating the exception in tSql()
catch SQLException in f()
finally in f()
catch te.f() SQLException in main
雖然在代碼(1)處重新拋出異常,但finally塊中的代碼仍然會被執(zhí)行。
2. finally造成的異常遺失
如果在finally中執(zhí)行的代碼又產(chǎn)生異常,那么在上一層調(diào)用中所捕捉到的異常的起始拋出點會是finally所在的函數(shù)。
import java.sql.SQLException;
class TestException{
public static void tSql1() throws SQLException {
System.out.println("Originating the exception in tSql()");
throw new SQLException("throw in tSql1");
}
public static void tSql2() throws SQLException {
System.out.println("Originating the exception in tSql()");
throw new SQLException("throw in tSql2");
}
public void f() throws SQLException{
try{
tSql1();
}
catch(SQLException ex){
System.out.println("catch SQLException in f()");
throw ex;//(2)
}
finally{
System.out.println("finally in f()");
//tSql2();(1)
}
}
}
public class Test{
public static void main(String[] args){
TestException te = new TestException();
try{
te.f();
}
catch(SQLException ex){
System.out.println("catch te.f() SQLException in main");
System.out.println("getMessage:" + ex.getMessage());
System.out.println("printStackTrace:");
ex.printStackTrace();
}
}
}
運行結(jié)果為:
Originating the exception in tSql()
catch SQLException in f()
finally in f()
catch te.f() SQLException in main
getMessage:throw in tSql1
printStackTrace:
java.sql.SQLException: throw in tSql1
void TestException.tSql1()
Test.java:5
void TestException.f()
Test.java:13
void Test.main(java.lang.String[])
Test.java:29
從結(jié)果可以看出,在main()中能正確打印出所捕捉到的異常的起始拋出點。但如果去掉代碼(1)的注釋,結(jié)果將變?yōu)椋?br>
Originating the exception in tSql()
catch SQLException in f()
finally in f()
Originating the exception in tSql()
catch te.f() SQLException in main
getMessage:throw in tSql2
printStackTrace:
java.sql.SQLException: throw in tSql2
void TestException.tSql2()
Test.java:9
void TestException.f()
Test.java:21
void Test.main(java.lang.String[])
Test.java:29
從結(jié)果可以看出,在main()中捕捉到的異常是finally中產(chǎn)生的異常,代碼(2)中拋出的異常丟失了。