Reputation: 15
I wrote a program who simulate a card game. I choose some values for the colors of the card and some values for values of the cards.
I wrote my own Exception, is CarteException.java
who have two child, CarteValeurException.java
and CarteCouleurException.java
depending on the type of Exception at the initialisation of a Carte
If the value of the color is not in 1 or 4, that will be throw an Exception of CarteCouleurExeception.java
and same for the other Exception, if the value are not 1 or in 7 and 13, that will be throw a CarteValeurException.java
This is my code for the class 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;
}
This is the code for the Class 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.");
}
}
And finally, the initialisation of a card in 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é");
}
}
The problem is in Belote.java
, Eclipse send me an error like this here catch(CarteException CE)
:
Unreachable catch block for CarteException. This exception is never thrown from the try statement body
But i put right : public Carte(int coul, int val) throws CarteException
in the first class so don't understand the problem.
I wrote the class CarteValeurException.java
and CarteCouleurException.java
but that is practically the same than CarteException.java
so that's why i don't put the code of the class right here.
Thank you for your help !
Upvotes: 0
Views: 105
Reputation: 73
You have to throw or catch your exception.
The signature of your method is:
private void initialiserTasDistribution() throws CarteException
So when the exception is trowed from the constructor in Carte is re-throwed by your method. Thus the catch is unreachable. You have to chose the best approach for your code. Here a set of rules of thumb: Throws or try+catch
Upvotes: 1
Reputation: 44240
Your problem is that your method is declared to propagate the exception higher up the call stack:
private void initialiserTasDistribution() throws CarteException {
This means that whoever calls the method initialiserTasDistribution
will have to handle this exception instead (or do the same and propagate it even higher). It is a form of deferring responsibility. It's saying "I'm not going to handle this error, someone else can".
This directly contradicts the implementation of your method because you do in fact handle the error.
try {
this.tasDistibution.push(new Carte(i,j));
}
catch(CarteException CE) {
System.err.println("Erreur : " + CE);
}
If you change your method signature to remove the throws
declaration, you should be okay:
private void initialiserTasDistribution() {
Upvotes: 0