Je crée une classe générique Pair
destinée aux entiers.Besoin d'aide pour attraper une exception
Exception Classe:
public class WrongThing extends Exception{
public WrongThing(String message) {
super(message);
}
}
Classe principale:
public class Pair<E> implements Comparable<E>{
private E var1;
private E var2;
public Pair(E var1, E var2) throws WrongThing{
//this is on purpose!!!
System.out.println("var1 and var2 are switched");
this.var1 = var2;
this.var2 = var1;
}
void get(){
System.out.println("the first actualy 2nd: "+
var1 + "the ");
System.out.println(" second actualy first" + var2);
}
void set1(E temp){
System.out.println("var1 and var2 are switched");
temp = var1;
}
void set2(E temp){
System.out.println("var1 and var2 are switched");
temp = var2;
}
E smallest(E var1, E var2){
return var1;
}
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
}
test cas
import java.util.Scanner;
import java.util.InputMismatchException;
public class PairTest {
public static void main(String[] args) throws WrongThing{
System.out.println("two integers please");
Scanner sc = new Scanner(System.in);
Pair<Integer> newPair;
Pair<Integer> tempPair1= new Pair<Integer>(3,2);
try{
newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());
//throw new InputMismatchException("that is not an Integer....");
}catch(WrongThing exception){
//System.out.println("you cant do that. try again and press enter after the first integer");
newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());
newPair.get();
}
finally{
}
}
//newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());
}
Quand je lance ce code je reçois un InputMismatchException
. N'ai-je pas créé mon exception correctement ou ne l'ai pas attrapé quand il a été lancé?
J'ai supprimé la balise Eclipse, car ce problème n'est pas lié à l'IDE que vous utilisez: c'est un problème Java "pur". –