java序列化意外地工作?

Sha*_*eer 0 java serialization

嗨如果一个类不能实现可序列化的接口,如果我们尝试序列化它,我们应该得到一个NotSerializableException.在这里,我没有得到它.本Cat类不落实Serializable的,我尝试序列化.编译并运行良好为什么?

 import java.io.*;
 class Cat{}
 class MyTest{
 public static void main(String a[]){
    Cat c = new Cat();
    try{
        FileOutputStream fos = new FileOutputStream("test.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(c);
        oos.close();
        fos.close();
    }
    catch(Exception e){e.getMessage();}
    try{
        FileInputStream fis = new FileInputStream("test.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        c =  (Cat)ois.readObject();
        ois.close();
        fis.close();
    }
    catch(Exception e){e.getMessage();}
}
}
Run Code Online (Sandbox Code Playgroud)

Sot*_*lis 5

它抛出异常,你只是吞下它们

catch(Exception e){e.getMessage();}
Run Code Online (Sandbox Code Playgroud)

它只调用一个返回String的方法,它不记录任何东西.

使用

e.printStackTrace();
Run Code Online (Sandbox Code Playgroud)