Error handling
- The program must emit an
UnavailableException
that is defined in Java by a class than inherits from Exception
:
public class UnavailableException extends Exception { }
- Next, the
buy
operation has to throws
this exception when necessary:
public void buy(String name,int quantity) {
for(Product book : this) { // find product
if (book.getName().equals(name)) { // String equality
if (book.getQuantity()>=quantity) {
book.buy(quantity);
return; // exit operation
}
// Here: bad quantity !
throw new UnavailableException();
}
}
// Here: product not found ! SEE EXERCICES
}
go further
- And the operation has to notify that an exception can occur:
public void buy(String name,int quantity) throws UnavailableException { ... }
2 - 12Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented
Java jva jav laurent thiry uha enseignant ensisa ecole school national alsace programmation objet approche mulhouse france java object programming oriented