0

i Have the following code but i am confused how i throw the InvalidLockCombinationException. This
exception (which should not have any methods) will indicate that an attempt to assign a
combination to a lock failed (thus making the combination invalid for that lock). If the combination is invalid (if not all its numbers are in the dial) then the constructor should throw a new InvalidLockCombinationException. By throwing this exception we can avoid creating locks with
invalid combinations (which in real life will be defective). All locks are open when created this is what i have so far. Any help would be appreciated on how to get the exception working.

    public class Lock{
    public Combination correct;
    public int upperLimit;
    public boolean isOpen;

    public Lock(int aLimit, Combination aCombo) throws InvalidLockCombinationException(){
    correct = aCombo;
    upperLimit = aLimit;
    isOpen=true;    
    int[] comboHolder = new int[3];
    comboHolder = aCombo.getNumbers();      
        for(int i=0; i<comboHolder.length;i++){
            if(comboHolder[i]<0 || comboHolder[i]>upperLimit){
                throw InvalidLockCombinationException;


}
}   
}
}
4

1 回答 1

1

例外也是一个对象。你不能真的扔一个球的想法,你需要一个实际的球来扔。

 throw new InvalidLockCombinationException();

既然你说它应该是一个没有额外字段或方法的基本异常,就给出了空构造函数。我仍然会尝试接受 String 消息以获取额外信息,但这超出了此答案的范围。

我建议使用实际的 IDE。它会礼貌地告诉你它的红色标记发生了什么。

于 2013-10-16T23:41:50.493 回答