【问题标题】:Throw Exception in constructor of a subclass在子类的构造函数中抛出异常
【发布时间】:2021-01-02 12:28:39
【问题描述】:

所以我的问题是我有一个类及其子类。

-> 类卡森邦 -> 类 KassenbonVerbessert 扩展 Kassenbon

在我的“Kassenbon”类中,我有这个构造函数:

   public Kassenbon(int max) {
        produkte = new String[max];
        preise = new Integer[max];
    }

然后我的子类的构造函数是这样的:

   public KassenbonVerbessert(int max) {
        super(max);
    }

我现在的问题是我想检查参数 max,如果它是负数,那么我想抛出一个异常,因为这意味着构造函数将创建 2 个负长度的数组,这是不可能的。但是我该怎么做,因为超级调用必须是构造函数中的第一个语句,对吗?!但是我该如何实现:

if(max < 0){
   throw new IllegalArgumentException("Error");
}

【问题讨论】:

  • 你为什么不把检查放在Kassenbon构造函数中?

标签: java exception constructor


【解决方案1】:

首先,在Kassenbon 构造函数中,该检查似乎更有意义。但是如果你不想这样做,你可以内联一个方法调用来检查参数:

public KassenbonVerbessert(int max) {
    super(checkMax(max));
}

private static int checkMax(int max) {
    if (max < 0) {
        throw new IllegalArgumentException("max cannot be negative.");
    }
    return max;
} 

【讨论】:

  • 非常感谢,这对我很有帮助。忘了提到我无法调整父类,所以我在超级调用中调用的另一种方法的解决方案是一个很好的解决方案 tbh
  • 如果这个答案解决了你的问题,你可以选择accepting the answer
【解决方案2】:

没有强制您需要将检查放在子类中。你可以直接把check放在你父类的构造函数里面。

【讨论】:

  • 是的,我忘了说我不能调整父类
【解决方案3】:

当您无法将检查应用于父构造函数时,您必须按照建议的herehere 内联检查。但是您不必为此编写自己的方法,您可以为此使用java.util.Objects.checkIndex()

public KassenbonVerbessert( int max ) 
{
    super( java.util.Objects.checkIndex( max, Integer.MAX_VALUE ) );
}

第二个参数是max 的最大值,如果你也想提供上边框的话……

当然,您可以为该方法添加静态导入:import static java.util.Objects.checkIndex;

【讨论】:

    【解决方案4】:

    除了@khelwood 所说的,您还可以使用工厂方法强制创建一个类。 通常当对象的创建变得更加复杂时——尤其是在检查和异常方面——你开始使用这些:

    package stackoverflow.kassensystem;
    
    public class Kassenbon {
    
        private final String[]  produkte;
        private final Integer[] preise;
    
        public Kassenbon(final int max) {
            produkte = new String[max];
            preise = new Integer[max];
        }
    
    }
    

    package stackoverflow.kassensystem;
    
    public class KassenbonVerbessert extends Kassenbon {
    
        static public KassenbonVerbessert createInstance(final int max) {
            if (max < 0) throw new IllegalArgumentException("Parameter 'max' is out of valid bounds! Expected max>0, but got '" + max + "' instead.");
            return new KassenbonVerbessert(max);
        }
    
        private KassenbonVerbessert(final int max) {
            super(max);
        }
    
    }
    

    使用方法:

    package stackoverflow.kassensystem;
    
    public class KassenSystem {
    
        public static void main(final String[] args) {
            System.out.println("KassenSystem.main() startet...");
            // KassenbonVerbessert kb1 = new KassenbonVerbessert(20); this line cannot work, because KassenbonVerbessert CTOR is private. Must instanciate via static method
            final KassenbonVerbessert kb2 = KassenbonVerbessert.createInstance(20); // this works alright
            final KassenbonVerbessert kb3 = KassenbonVerbessert.createInstance(-666); // this will fail
        }
    
    }
    

    【讨论】:

    • 哦,你的例外越详细越好。以后你会感谢自己的。只要确保你没有在异常中包含密码和其他秘密,因为在 Java 中你不能完全控制异常处理是如何完成的;它很容易受到外界的影响。
    猜你喜欢
    • 2014-11-03
    • 2017-02-09
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多