2
在此Java程序示例:試穿與資源語句的Java
package test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
public class Test
{
private static void example(){
String url = "jdbc:oracle:thin:@//localhost:7856/xe";
String user = "user";
String password = "pass";
try(Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement()){
throw new OutOfMemoryError("Error");
}catch (SQLException e){
System.err.println("SQLException");
}
}
public static void main(String [] args){
try{
example();
}catch (OutOfMemoryError e){
System.err.println("OutOfMemoryError");
}
// Rest of code here...
}
}
時,在靜態方法示例()的機構,OutOfMemoryError異常錯誤被拋出,是連接「CON」和聲明「stmt」在終止靜態方法example()之前自動關閉,儘管沒有任何「catch」捕獲這些錯誤,所以在main()的其餘代碼中確定這兩個對象是關閉的?
謝謝。