[英]My own Exception doesn't work
我寫了一個模擬紙牌游戲的程序。 我為卡的顏色選擇了一些值,為卡的值選擇了一些值。
我寫了我自己的Exception,是CarteException.java
,它有兩個孩子CarteValeurException.java
和CarteCouleurException.java
這取決於在Carte
初始化時的Exception類型。
如果顏色的值不在1或4中,則將引發CarteCouleurExeception.java
的異常,對於其他異常,則將其引發;如果值不是1或7和13,則將引發CarteValeurException.java
這是我的Carte.java
類的Carte.java
:
public Carte(int coul, int val) throws CarteException {
if(coul < 1 || coul > 4) {
throw new CarteCouleurException("Erreur d'initialisation lors de la création d'une carte.",coul);
}
if(val < 1 || (val > 1 && val < 7) || val > 13) {
throw new CarteValeurException("Erreur d'initialisation lors de la création d'une carte.",val);
}
this.couleur = coul;
this.valeur = val;
}
這是類CarteException.java
的代碼:
public class CarteException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
protected String message;
protected CarteException() {
this.message = "";
}
public CarteException(String chaine) {
this.message = chaine;
}
public String getMessage() {
return (this.message + " : Erreur non spécifiée de valeur ou de couleur de carte.");
}
}
最后,在Belote.java
初始化卡:
package TP.TP6.Exercice2;
import java.util.Random;
import java.util.Vector;
import java.util.Stack;
import TP.TP5.Exercice1.Question4.Carte;
public class Belote {
private Stack<Carte> tasDistibution;
private Vector<Stack<Carte>> mainsJoueurs;
private Vector<Stack<Carte>> plisJoueurs;
public Belote() {
this.tasDistibution = new Stack<Carte>();
this.mainsJoueurs = new Vector<Stack<Carte>>(4);
this.plisJoueurs = new Vector<Stack<Carte>>(4);
for(int i = 0 ; i < 4 ; i++) {
this.mainsJoueurs.add(i, new Stack<Carte>());
this.plisJoueurs.add(i, new Stack<Carte>());
}
}
private void initialiserTasDistribution() throws CarteException {
for (int i = 0; i <= 5; i++) {
for (int j = 1; j <= 13; j++) {
try {
//Initialisation right here
this.tasDistibution.push(new Carte(i,j));
}
catch(CarteException CE) {
System.err.println("Erreur : " + CE);
}
}
}
}
private void couper() {
Stack<Carte> tas1 = new Stack<Carte>();
Stack<Carte> tas2 = new Stack<Carte>();
Random r = new Random();
int coupe = 1 + r.nextInt(33 - 1);
for (int i = 0; i < coupe; i++) {
Carte carte = this.tasDistibution.peek();
this.tasDistibution.pop();
tas1.push(carte);
}
while (tasDistibution.isEmpty() == false) {
Carte carte = this.tasDistibution.peek();
this.tasDistibution.pop();
tas2.push(carte);
}
while (tas1.isEmpty() == false) {
Carte carte = tas1.peek();
tas1.pop();
this.tasDistibution.push(carte);
}
while (tas2.isEmpty() == false) {
Carte carte = tas2.peek();
tas2.pop();
this.tasDistibution.push(carte);
}
}
private void melanger(int nbMelange) {
Carte tabcarte[] = new Carte[32];
for (int i = 0; i < tabcarte.length; i++) {
Carte cartesommet = this.tasDistibution.peek();
this.tasDistibution.pop();
tabcarte[i] = cartesommet;
}
for (int i = 0; i < nbMelange; i++) {
Random r = new Random();
int pos1 = 1 + r.nextInt(32 - 1);
int pos2 = 1 + r.nextInt(32 - 1);
if (pos1 == pos2) {
System.out.println("Pas de chance");
} else {
Carte temp;
temp = tabcarte[pos1];
tabcarte[pos1] = tabcarte[pos2];
tabcarte[pos2] = temp;
}
}
for (int i = 0; i < tabcarte.length; i++) {
Carte carte = tabcarte[i];
this.tasDistibution.push(carte);
}
}
private void donnerCartesAJoueur(int nbcartedonnes, int numjoueur) {
for (int i = 0; i < nbcartedonnes; i++) {
Carte carte = this.tasDistibution.peek();
this.tasDistibution.pop();
Stack<Carte> stack = this.mainsJoueurs.get(numjoueur);
stack.push(carte);
this.mainsJoueurs.set(numjoueur, stack);
}
}
private void distribuer() {
for (int i = 0; i < 4; i++) {
this.donnerCartesAJoueur(3, i);
}
for (int i = 0; i < 4; i++) {
this.donnerCartesAJoueur(2, i);
}
for (int i = 0; i < 4; i++) {
this.donnerCartesAJoueur(3, i);
}
for (int i = 0; i < 4; i++) {
System.out.println("\n\nDistribution pour joueur : " + (i+1) + " \n\nMain du joueur : " + (i+1));
System.out.println(this.mainsJoueurs.get(i).toString());
}
}
private void assemblerPlisJoueur() {
for (int i = 0; i < 4; i++) {
while (this.plisJoueurs.get(i).isEmpty() == false) {
Carte carte = this.plisJoueurs.get(i).peek();
Stack<Carte> stack = this.plisJoueurs.get(i);
stack.pop();
this.plisJoueurs.set(i, stack);
this.tasDistibution.push(carte);
}
}
}
private void preparerPremiereManche() throws CarteException {
try {
this.initialiserTasDistribution();
}
catch(CarteException CE) {
System.err.println("Erreur d'initilisation du tas à distribuer.");
throw CE;
}
this.melanger(32);
this.couper();
this.distribuer();
}
private void preparerMancheSuivante() {
this.assemblerPlisJoueur();
this.couper();
this.distribuer();
}
private void jouerPli() {
Stack<Carte> tasIntermediaire = new Stack<Carte>();
for (int i = 0; i < 4; i++) {
Carte carte = this.mainsJoueurs.get(i).peek();
Stack<Carte> stack = this.mainsJoueurs.get(i);
stack.pop();
this.mainsJoueurs.set(i, stack);
tasIntermediaire.push(carte);
}
Random r = new Random();
int gagnant = 0 + r.nextInt(4 - 0);
System.out.println("Le joueur " + (gagnant+1) + " a gagné ce pli");
for (int i = 0; i < 4; i++) {
Carte carte = tasIntermediaire.peek();
tasIntermediaire.pop();
Stack<Carte> stack = this.plisJoueurs.get(gagnant);
stack.push(carte);
this.plisJoueurs.set(gagnant, stack);
}
System.out.println("Pli du joueur " + (gagnant+1));
System.out.println(this.plisJoueurs.get(gagnant).toString());
}
private void jouerManche(int nbPlis) {
for (int i = 1; i <= nbPlis; i++) {
System.out.println("\n\nPli numéro : " + i);
this.jouerPli();
}
this.preparerMancheSuivante();
}
public void jouerPartie(int nbManches) throws CarteException {
try {
this.preparerPremiereManche();
}
catch(CarteException CE) {
System.err.println("Erreur d'initialisation de la première manche");
throw CE;
}
for (int i = 1; i <= nbManches; i++) {
System.out.println("\n\nManche numéro : " + i);
this.jouerManche(8);
}
System.out.println("Jeu terminé");
}
}
問題出在Belote.java
,Eclipse在這里向我發送了一個錯誤,例如catch(CarteException CE)
:
CarteException的不可用catch塊。 永遠不會從try語句主體中拋出此異常
但是我說對了: public Carte(int coul, int val) throws CarteException
在第一類中public Carte(int coul, int val) throws CarteException
,所以不理解問題。
我編寫了CarteValeurException.java
和CarteCouleurException.java
類,但是實際上與CarteException.java
相同,所以這就是為什么我不在此處放置該類的代碼。
謝謝您的幫助 !
您必須拋出或捕獲異常。
您方法的簽名是:
private void initialiserTasDistribution() throws CarteException
因此,當從Carte的構造方法中拋出異常時,您的方法將重新拋出該異常。 因此,漁獲量無法達到。 您必須為代碼選擇最佳方法。 這是一套經驗法則: 投擲或try + catch
您的問題是您的方法被聲明為將異常傳播到更高的調用堆棧:
private void initialiserTasDistribution() throws CarteException {
這意味着,調用方法initialiserTasDistribution
任何人都必須改為處理此異常(或執行相同的操作並將其傳播到更高的層次)。 這是一種延遲責任的形式。 有人說“我不會處理這個錯誤,其他人可以。”
這直接與方法的實現相矛盾,因為實際上您確實在處理錯誤。
try {
this.tasDistibution.push(new Carte(i,j));
}
catch(CarteException CE) {
System.err.println("Erreur : " + CE);
}
如果您更改方法簽名以刪除throws
聲明,則應該可以:
private void initialiserTasDistribution() {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.