# try()的小括號裡面的規則 ```java= class DataConverter{ public void copyFlatFilesToTables() { } public void close() throws Exception{ throw new RuntimeException(); } } public class Test { public static void main(String[] args) { try(DataConverter dc = new DataConverter()){ dc.copyFlatFilesToTables(); } } } ``` **程式會錯在第12行,顯示DataConverter必須要implement AutoCloseable** --- **程式修改後:** ```java= class DataConverter implements AutoCloseable{ public void copyFlatFilesToTables() { } public void close() throws Exception{ throw new RuntimeException(); } } public class Test { public static void main(String[] args) { try(DataConverter dc = new DataConverter()){ dc.copyFlatFilesToTables(); } catch (Exception e) { e.printStackTrace(); } } // DataConverter 要implement AutoCloseable 才能寫在try的小括號 } ``` :::warning :bulb: DataConverter 要implement AutoCloseable 才能寫在try的小括號裡面 ::: ###### tags: `ocpjp`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up