Java 日看一类(59)之 IO包中的WriteAbortedException和UTFDataFormatException异常类

本文介绍了两种序列化过程中可能遇到的异常:WriteAbortedException和UTFDataFormatException。前者用于标记序列化写操作失败的情况,后者则是在读取modified UTF-8格式的字符串时发生错误时抛出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WriteAbortedException无引入包

继承自ObjectStreamException



该类的类头注释如下:

/**
 * Signals that one of the ObjectStreamExceptions was thrown during a
 * write operation.  Thrown during a read operation when one of the
 * ObjectStreamExceptions was thrown during a write operation.  The
 * exception that terminated the write can be found in the detail
 * field. The stream is reset to it's initial state and all references
 * to objects already deserialized are discarded.
 *
 * <p>As of release 1.4, this exception has been retrofitted to conform to
 * the general purpose exception-chaining mechanism.  The "exception causing
 * the abort" that is provided at construction time and
 * accessed via the public {@link #detail} field is now known as the
 * <i>cause</i>, and may be accessed via the {@link Throwable#getCause()}
 * method, as well as the aforementioned "legacy field."
 *
 * @author  unascribed
 * @since   JDK1.1
 */

大意如下:

标志着一个ObjectStreamException在写出操作时被抛出

在写入操作过程中抛出 ObjectStreamExceptions 之一时,在读取操作过程中将抛出此异常。终止写入的异常可在详细信息字段中找到。该流被重置为初始状态,而且对已经反序列化的对象的所有引用都被丢弃。

从版本 1.4 开始,已对此异常作出改进,以符合通用异常链机制。在构造时提供并通过公共 detail 字段访问的“导致中止的异常”现在称为 cause,并可通过 Throwable.getCause() 方法以及前述的“遗留字段”访问



该类含有如下的成员变量:

序列化ID

private static final long serialVersionUID = -3326426625597282442L;

异常信息记录

public Exception detail;


该类含有如下的成员方法:

构造方法(异常原因和需要打印出的提示

public WriteAbortedException(String s, Exception ex) {
    super(s);
    initCause(null);  // Disallow subsequent initCause
    detail = ex;
}

获得异常详细原因(字符串

public String getMessage() {
    if (detail == null)
        return super.getMessage();
    else
        return super.getMessage() + "; " + detail.toString();
}

获得异常详细原因(异常类

public Throwable getCause() {
    return detail;
}



该类主要用在记录序列化操作错误,记得使用即可



UTFDataFormatException无引入包

继承自IOException


该类的类头注释如下:

/**
 * Signals that a malformed string in
 * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
 * format has been read in a data
 * input stream or by any class that implements the data input
 * interface.
 * See the
 * <a href="DataInput.html#modified-utf-8"><code>DataInput</code></a>
 * class description for the format in
 * which modified UTF-8 strings are read and written.
 *
 * @author  Frank Yellin
 * @see     java.io.DataInput
 * @see     java.io.DataInputStream#readUTF(java.io.DataInput)
 * @see     java.io.IOException
 * @since   JDK1.0
 */

大意如下:

在数据输入流中或由实现该数据输入接口的任何类中以 UTF-8 修改版格式读取错误字符串时,抛出此异常。有关读取和写入 UTF-8 修改版字符串的格式,请参阅 DataInput 类的描述。


该类含有如下的成员变量:

序列化ID:

private static final long serialVersionUID = 420743449228280612L;


该类含有如下的成员方法:

构造方法(默认

public UTFDataFormatException() {
    super();
}

构造方法(带错误信息

public UTFDataFormatException(String s) {
    super(s);
}


该类如注释所说主要用在modified UTF-8类型的字符读取中,注意即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值