import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Forest {
private Tree tree = new Tree();
public static void main(String[] args){
Forest f = new Forest();
try {
FileOutputStream fs = new FileOutputStream("Forest.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(f);
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Tree{}
---------------------------------
java.io.NotSerializableException: Forest
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Forest.main(Forest.java:13)
---------------------------------
FileOutputStream fs = new FileOutputStream("Forest.ser");
需要加入 try/catch 或 丟出例外 FileNotFoundException e
ObjectOutputStream os = new ObjectOutputStream(fs);
需要加入 try/catch 或 丟出例外 IOException e
因此綜合兩個例外,選擇 Exception