使用自定义例外

时间:2016-02-11 21:32:27

标签: java exception-handling custom-exceptions

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

这是我的卡片类的一个例子。我需要能够在我的异常类中访问getSuitDesignator。

    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 个答案:

答案 0 :(得分:2)

您需要扩展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
    }
}