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

Java开发Minecraft模组:Exception异常抛出失效问题排查

Minecraft模组开发Java异常触发问题排查与修复

问题描述

我正在用Java开发Minecraft模组,实现异常抛出逻辑时遇到问题。尝试过这些方案:

  • 使用Map替代List存储实例
  • 尝试Try-Catch捕获异常
  • 通过For循环遍历获取ID

需求是调用register方法时,只要任一if条件成立就抛出异常,但实际完全没有触发,仿佛这些if条件永远为false。

相关代码

SoulType类

public class SoulType {
    private static final List<SoulType> SOUL_TYPES = new ArrayList<>();

    private final String id;
    private final String displayName;
    private final SoulTypeRarities rarity;
    private final double corruption;
    private double strength;

    private SoulType(String id, String displayName, SoulTypeRarities rarity, double corruption, double strength) {
        this.id = id;
        this.displayName = displayName;
        this.rarity = rarity;
        this.corruption = corruption;
        this.strength = strength;
    }

    public static List<SoulType> getSoulTypes() { // Neue Getter-Methode
        return SOUL_TYPES;
    }

    public String getId() {
        return id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public SoulTypeRarities getRarity() {
        return rarity;
    }

    public Double getCorruption() {
        return corruption;
    }

    public Double getStrength() {
        return strength;
    }

    public void setStrength(double newStrength) {
        this.strength = newStrength;
    }

    public void addStrength(double addStrength) {
        this.strength += addStrength;
    }

    public static SoulType register(String id, String displayName, SoulTypeRarities rarity, double corruption, double strength) throws IllegalArgumentException {
        double minCorruption = 0;
        double maxCorruption = 2;
        double minStrength = 0;
        double maxStrength = 5;
        if (corruption < minCorruption || corruption > maxCorruption) {
            throw new IllegalArgumentException("Corruption must be between " + minCorruption + " and " + maxCorruption + " .");
        }
        if (strength < minStrength || strength > maxStrength) {
            throw new IllegalArgumentException("Strength must be between " + minStrength + " and " + maxStrength + " .");
        }
        SoulType newType = new SoulType(id, displayName, rarity, corruption, strength);
        SOUL_TYPES.add(newType);
        for (SoulType soulType : SOUL_TYPES) {
            if (soulType.getId().equals(id)) {
                throw new IllegalArgumentException("SoulType with id '" + id + "' already exists.");
            }
        }
        return newType;
    }

    public static SoulType fromId(String id) {
        for (SoulType soulType : SOUL_TYPES) {
            if (soulType.getId().equals(id)) {
                return soulType;
            }
        }
        throw new IllegalArgumentException("SoulType with id '" + id + "' not found.");
    }
}

调用类SoulTypes

public class SoulTypes {
    public static final SoulType HARMONY = SoulType.register("harmony", "Harmonie", SoulTypeRarities.COMMON, 0.0, 1.0);
    public static final SoulType BOLLWERK = SoulType.register("bollwerk", "Bollwerk", SoulTypeRarities.RARE, 0.8, 1.5);
}

问题分析

  1. 数值校验未触发的原因:你当前传入的corruption和strength参数都在设定的合法范围内(corruption 0-2,strength 0-5),所以这两个if条件自然不会触发异常。只有传入超出范围的值时,才会触发对应的异常。
  2. ID重复校验的逻辑错误:register方法中,你先将新创建的SoulType实例添加到列表,再遍历列表检查ID重复——这会导致每次注册都会抛出ID重复异常,但你说没触发,说明你还没注册重复ID的实例,或者该逻辑顺序完全错误:正确的流程应该是先检查ID是否重复,再创建实例并添加到列表。

修复方案

修改register方法的逻辑顺序,将ID重复检查放到实例创建和添加之前,同时保留数值范围校验:

public static SoulType register(String id, String displayName, SoulTypeRarities rarity, double corruption, double strength) throws IllegalArgumentException {
    double minCorruption = 0;
    double maxCorruption = 2;
    double minStrength = 0;
    double maxStrength = 5;
    
    // 1. 先校验数值范围
    if (corruption < minCorruption || corruption > maxCorruption) {
        throw new IllegalArgumentException("Corruption must be between " + minCorruption + " and " + maxCorruption + ".");
    }
    if (strength < minStrength || strength > maxStrength) {
        throw new IllegalArgumentException("Strength must be between " + minStrength + " and " + maxStrength + ".");
    }
    
    // 2. 检查ID是否已存在
    for (SoulType soulType : SOUL_TYPES) {
        if (soulType.getId().equals(id)) {
            throw new IllegalArgumentException("SoulType with id '" + id + "' already exists.");
        }
    }
    
    // 3. 创建实例并添加到列表
    SoulType newType = new SoulType(id, displayName, rarity, corruption, strength);
    SOUL_TYPES.add(newType);
    
    return newType;
}

测试方法

  • 测试数值范围校验:调用SoulType.register("test", "Test", SoulTypeRarities.COMMON, -0.5, 1.0),此时corruption小于最小值,应该抛出对应的异常。
  • 测试ID重复校验:先注册一次"harmony",再调用一次相同ID的register方法,应该抛出ID重复的异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 01:00:55