ObjectStreamException异常类继承自IOException
该类的类头注释如下:
* Superclass of all exceptions specific to Object Stream classes.
大意如下:
描述所有对象流类异常的顶级类
该类含有如下的成员变量:
序列化ID
private static final long serialVersionUID = 7260898174833392607L;
该类含有如下的成员方法:
构造方法(传入出错的类名)
protected ObjectStreamException(String classname) {
super(classname);
}
构造方法(默认)
protected ObjectStreamException() {
super();
}
该类作为对象流异常的顶级类,一般情况下被使用到的是其子类,如下面要提到的OptionalDataException
OptionalDataException继承自ObjectStreamException
该类的类头注释如下:
/**
* Exception indicating the failure of an object read operation due to
* unread primitive data, or the end of data belonging to a serialized
* object in the stream. This exception may be thrown in two cases:
*
*
*
An attempt was made to read an object when the next element in the* stream is primitive data. In this case, the OptionalDataException's
* length field is set to the number of bytes of primitive data
* immediately readable from the stream, and the eof field is set to
* false.
*
*
An attempt was made to read past the end of data consumable by a* class-defined readObject or readExternal method. In this case, the
* OptionalDataException's eof field is set to true, and the length field
* is set to 0.
*
*
* @author unascribed
* @since JDK1.1
*/
大意如下:
该类表示读取对象操作失败,原因是无法读取流中对象的基本数据或者是序列化对象的末尾数据,该异常会因两种原因被抛出:
企图读取的对象下一部分是基础数据,在这种情况下,OptionalDataException的长度字段将会被设置为立刻可从流中读出的基础数据字节数,文件结束字段为false
企图使用类声明的readObject或者readExternal方法来读取数据末尾后的部分。在这种情况下,文件结束字段会被设置为true,并且长度字段设置为0
该类含有如下的成员变量:
序列化ID
private static final long serialVersionUID = -8011121865681257820L;
可立刻被读取出的byte长度
public int length;
文件结束标志符
public boolean eof;
该类含有如下的成员方法:
构造函数(第一种情况
OptionalDataException(int len) {
eof = false;
length = len;
}
构造函数(第二种情况
OptionalDataException(boolean end) {
length = 0;
eof = end;
}
该类的使用方式在注释中已经提到,在这里不再赘述(该类的注释写的有点模糊,说是数据末尾,又说是数据末尾后的非法空间,等到看到了该类的具体抛出位置再来补充和更新)