异常类

基本信息

属性 值
位置 src/main/java/mytown/util/exceptions/
异常数 7
启用版本 Minecraft 1.7.10

本 mod 自定义的全部异常类型。⚠️ 它们不是同一种基类,分 checked / unchecked 两类。

异常 基类 种类 抛出位置
ConditionException java.lang.Exception checked Condition —— 条件串非法
GetterException java.lang.Exception checked Getter 取值失败
ConfigException RuntimeException unchecked 配置相关
SegmentException RuntimeException unchecked Segment 规则段失败
MyTownCommandException net.minecraft.command.CommandException unchecked Commands 体系
MyTownWrongUsageException net.minecraft.command.WrongUsageException unchecked 命令参数用法错误
ProtectionParseException com.google.gson.JsonParseException unchecked ProtectionParser

实现要点

⚠️ 只有 ConditionException 与 GetterException 是 checked(直接 extends Exception), 因此 Condition 的 execute 签名必须声明:

public boolean execute(Object object, Getter.Container getters)
    throws ConditionException, GetterException

这是全 mod 唯一在方法签名上显式 throws 的判定方法。 其余 5 个 unchecked 异常靠运行时传播,不需要 throws 声明。

⚠️ 后 3 个异常继承的是外部基类而非 RuntimeException: CommandException / WrongUsageException 来自 Minecraft 原版命令框架, JsonParseException 来自 Gson。 继承它们意味着调用方可直接复用原版命令/Gson 的既有 catch 逻辑。

⚠️ Condition.execute 的四类消息措辞不统一,不要混用:

场景 消息格式
比较符不支持 "The element number N has an invalid condition!"
元素类型无法解析 "The element with number N has an invalid type to be checked against!"(注意是 with number)
i+3 非 AND/OR "Invalid condition element: X"

⚠️ Protection 的 Serializer.deserialize 路径抛 ProtectionParseException;而 SellSign 的 isTileValid 把所有异常 catch 后返回 false(静默), 因此**「告示牌无效」不产生异常日志**。

相关条目