有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用自定义异常的java

我想知道如何在另一个类中使用定制的异常。例如,我正在制作一个卡片类和一个invalidCardException。如何在exception类内部使用Card类中的方法,反之亦然

这是我的卡片类的一个例子。我需要能够访问exception类中的GetSuiteDesigner

    public class Card {
    private int cardID;  //I chose to use a single variable that could mathematically be used to transform from and to the value and suit
    //However a far simpler method would have simply stored the value and suit exactly as given by the constructor

private final int NUMBER_OF_SUITS = 4;  //By using these constants we can alter the characteristics of the cards
private final int CARDS_PER_SUIT = 13;
private final char [] CARD_SUIT_CHARS = {'H', 'S', 'C', 'D'};  //Great idea from Melissa Bruno
private final String [] CARD_SUIT_NAMES = {"Heart", "Spade", "Club", "Diamond"};
private final String [] CARD_VALUE_NAMES = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};



//  private Card(): creates a blank card with no suit and no value
// 1: First method set written and tested
private Card()
{
}

// Card(char suit, int value)  : creates a card of the specified suit (H, S, C, D) and value (1-13)
// 1: First method set written and tested
public Card(char inCardSuit, int inCardValue)
{
int suitSequence;

for(suitSequence = 0; suitSequence < this.NUMBER_OF_SUITS; suitSequence++)
{
if(this.CARD_SUIT_CHARS[suitSequence] == inCardSuit)
{
break;  //Suit found
}
}

this.cardID = (suitSequence * this.CARDS_PER_SUIT) + (inCardValue - 1);
}

//char getSuitDesignator() : returns char designator of card suit
// 4: Fourth method set written and tested 
public char getSuitDesignator()
{
int suitSequence = this.cardID / this.CARDS_PER_SUIT; //int Math to get the suit - originally had / and % flipped - found and fixed during testing
return this.CARD_SUIT_CHARS[suitSequence];
}

//int getValue() : returns card value (1-13)
// 4: Fourth method set written and tested 
public int getValue()
{
int cardValue = this.cardID % this.CARDS_PER_SUIT; //int Math to get the suit
return cardValue + 1; //Adding 1 to translate to a 1-13 card value (did not do this at first - found and fixed during testing)

}


//String getSuitName() : returns String name of card suit
// 2: Second method set written and tested (also altered toString to use this method
public String getSuitName()
{
int suitSequence = this.cardID / this.CARDS_PER_SUIT; //int Math to get the suit
return this.CARD_SUIT_NAMES[suitSequence];
}

//String getValueName() : returns String name of card value (i.e. “Ace”, “Two”, … , “Queen”, “King”)
// 3: Third method set written and tested (also altered toString to use this method
public String getValueName()
{
int valueSequence = this.cardID % this.CARDS_PER_SUIT; //int Math to get the suit
return this.CARD_VALUE_NAMES[valueSequence];

}

//String toString() :  returns a representation of the Card as <suit name> +  “ “ + <value name>
// 1: First method set written and tested
public String toString()
{
//return this.cardID;  // 1: As it was first written (simply for testing the constructor
//return this.getSuitName() + " " + this.cardID;  // 2: As altered for Second method set
return this.getSuitName() + " " + this.getValueName();  // 3: Final version
}

//boolean compareSuit(Card) : returns true if both cards have the same suit regardless of value
// 5: Fifth method set written and tested 
public boolean compareSuit(Card inCompareCard)
{
return inCompareCard.getSuitDesignator() == this.getSuitDesignator();
}

//boolean compareValue(Card) : returns true if both cards have the same value regardless of suit
// 6: Sixth method set written and tested 
public boolean compareValue(Card inCompareCard)
{
return inCompareCard.getValue() == this.getValue();
}

//boolean compareTo(Card) : returns true if both cards have the same value and suit
// 6: Sixth method set written and tested 
public boolean compareTo(Card inCompareCard)
{
return (this.compareValue(inCompareCard) && this.compareSuit(inCompareCard)); //reuse the methods already written -- less code and changes go everywhere
}


}

共 (1) 个答案

  1. # 1 楼答案

    您需要扩展Exception类。您可以创建一个接受Card对象的构造函数,并将Card类的方法用于该对象。请参见下面的示例:

    例如:

    public class InvalidCardException extends Exception {
        public InvalidCardException(String message) {
            super(message);
        }
    
          public InvalidCardException(String message, Card card) {
            super(message);
            // do something with card object
        }
    }
    

    如果需要用其他方法访问该对象,可以将该对象分配给实例变量

    例如:

    public class InvalidCardException extends Exception {
        private Card card;
    
        public InvalidCardException(String message) {
            super(message);
        }
    
        public InvalidCardException(String message, Card card) {
            super(message);
            this.card = card;
        }
    
        public void doSomethingWithCard() {
            // use card methods
        }
    }