2016-02-11 109 views
2

我想知道如何使用另一個類的自定義異常。例如,我正在製作一個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 
} 


} 
+0

您需要在自定義異常類擴展基本異常類,你可以在任何函數聲明與類型的自定義類的罰球。基於邏輯,你可以捕捉或進一步拋出。這僅適用於自定義異常類。 – pratikpawar

+0

你能否給我們舉個例子,說明你的Card類可能是什麼樣的,你需要從異常中訪問什麼方法?難道你只是將所需的值傳遞給異常構造函數嗎? - [這可能會有所幫助](http://stackoverflow.com/help/mcve) – radoh

回答

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 
    } 
}