illustration des exceptions personnalisés
Bonjour,
Voulant illustrer le principe des exceptions personnalisé, je me permets d'écrire le code suivant qui fonctionne pas!
En fait j'ai une class livre2 avec comme propriété titre et prix.
je veux que si l'utilisateur entre un prix negatif une exceptions soit lévée
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
class prixexceptions extends Exception {
public prixexceptions(){
System.out.println("le prix doit pas etre negatif");
}
}
public class livre2{
private String titre;
private double prix;
public livre2(String untitre, double unprix) throws prixexceptions{
if(unprix<0){
throw prixexceptions();
} else {
this.titre=untitre;
this.prix=unprix;
}
}
public livre2(){
this.titre="inconnu";
this.prix=0;
}
public String toString(){
String msg=" ";
msg+="Le titre:"+" "+this.titre+("\n");
msg+="Prix"+" "+this.prix;
return msg;
}
public static void main(String[] args){
livre2 l=null;
try{
l=new livre2("Booba",-200);
} catch (prixexceptions e){
l=new livre2();
}
//livre l =new livre();
System.out.println(l.toString());
}
} |
A la compilation j'ai deux erreurs que j'ai du mal à resoudre:
erreur1:
Code:
The serializable class prixexceptions does not declare a static final serialVersionUID field of type long
A la ligne 4 dont voici le code dans mon éditeur
Code:
class prixexceptions extends Exception {
erreur2:
Code:
The method prixexceptions() is undefined for the type livre2
a la ligne 14 dont voici le code
Code:
throw prixexceptions();
Bien merci pour votre aide