---
# System prepended metadata

title: try()的小括號裡面的規則
tags: [ocpjp]

---

# 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`