close

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

arrow
arrow
    全站熱搜

    pcwiki 發表在 痞客邦 留言(0) 人氣()