You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将队列空异常与满异常合并为一个QueueException类?

嘿,我懂你的纠结!之前写栈的时候分别搞了StackFullException和StackEmptyException,现在想把队列的空、满异常合并到一个QueueException里,其实有几种实用的实现方式,我给你拆解一下:

方法一:用枚举标记异常类型(推荐,严谨易扩展)

这种方式通过枚举明确区分异常类型,既把两种异常归到同一个类里,又能在捕获时精准判断是哪种情况,后期加新的队列异常类型也很方便。

先定义一个枚举来标识异常类型:

public enum QueueExceptionType {
    EMPTY, FULL
}

然后编写合并后的QueueException类:

public class QueueException extends Exception {
    // 保存当前异常的类型
    private final QueueExceptionType type;

    // 带类型和自定义消息的构造函数
    public QueueException(QueueExceptionType type, String message) {
        super(message);
        this.type = type;
    }

    // 只传类型的构造函数,用默认提示消息
    public QueueException(QueueExceptionType type) {
        this(type, type == QueueExceptionType.EMPTY ? "队列已空,无法执行出队操作" : "队列已满,无法执行入队操作");
    }

    // 提供获取异常类型的方法,方便捕获时判断
    public QueueExceptionType getType() {
        return type;
    }
}

使用示例:

// 抛出队列空异常
throw new QueueException(QueueExceptionType.EMPTY);
// 抛出队列满异常,带自定义消息
throw new QueueException(QueueExceptionType.FULL, "队列容量已达100,无法添加新元素");

// 捕获时区分处理
try {
    // 执行队列入队/出队操作
} catch (QueueException e) {
    if (e.getType() == QueueExceptionType.EMPTY) {
        System.out.println("处理空队列逻辑:" + e.getMessage());
    } else if (e.getType() == QueueExceptionType.FULL) {
        System.out.println("处理满队列逻辑:" + e.getMessage());
    }
}
方法二:简单构造函数区分(快速实现,适合无需精细处理的场景)

如果你的场景不需要严格区分异常类型,只是想把两种异常合并成一个类,直接通过不同的构造函数或静态方法返回带不同消息的异常即可,实现起来最简单:

public class QueueException extends Exception {
    // 默认构造函数
    public QueueException() {
        super("队列操作异常");
    }

    // 带自定义消息的构造函数
    public QueueException(String message) {
        super(message);
    }

    // 静态方法快速返回空队列异常
    public static QueueException emptyQueue() {
        return new QueueException("队列已空,无法执行出队");
    }

    // 静态方法快速返回满队列异常
    public static QueueException fullQueue() {
        return new QueueException("队列已满,无法执行入队");
    }
}

使用示例:

// 抛出空队列异常
throw QueueException.emptyQueue();
// 抛出满队列异常
throw QueueException.fullQueue();

// 捕获时可以统一处理,或通过消息字符串判断(注意:字符串判断不如枚举严谨)
try {
    // 队列操作
} catch (QueueException e) {
    System.out.println("队列异常:" + e.getMessage());
    if (e.getMessage().contains("已空")) {
        // 处理空队列逻辑
    }
}
方法三:父类+子类模式(兼顾灵活性和原有习惯)

如果你既想保留“一个QueueException大类别”,又想延续之前栈的写法(单独捕获空/满异常),可以让QueueException作为父类,再写两个子类继承它:

// 父类:统一的QueueException
public class QueueException extends Exception {
    public QueueException(String message) {
        super(message);
    }
}

// 子类:队列空异常
public class QueueEmptyException extends QueueException {
    public QueueEmptyException() {
        super("队列已空");
    }
}

// 子类:队列满异常
public class QueueFullException extends QueueException {
    public QueueFullException() {
        super("队列已满");
    }
}

使用示例:

// 抛出空异常
throw new QueueEmptyException();
// 抛出满异常
throw new QueueFullException();

// 捕获时可以单独处理子类异常,也可以统一捕获父类
try {
    // 队列操作
} catch (QueueEmptyException e) {
    // 专门处理空队列
} catch (QueueFullException e) {
    // 专门处理满队列
} catch (QueueException e) {
    // 处理其他队列异常
}

内容的提问来源于stack exchange,提问作者lio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:22:37