| ... | ... | @@ -125,7 +125,6 @@ public int view() throws StackIsEmptyException { ... } |
|
|
|
|
|
|
|
:book: La balise `@throws` permet de documenter dans quel cas l'exception est soulevée (une balise par exception)
|
|
|
|
|
|
|
|
:book: Les constructeurs peuvent aussi soulever des exceptions, dans ce cas **aucun objet n'est créé**.
|
|
|
|
# Soulevement des exceptions
|
|
|
|
|
|
|
|
:book: Pour avertir d'une anomalie lors de sa détection, une méthode :
|
| ... | ... | @@ -155,3 +154,26 @@ public void push(int element) throws StackIsFullException, NegativeElementExcept |
|
|
|
```
|
|
|
|
|
|
|
|
:bulb: `throw` est terminal (l'exécution se termine ici), comme `return`, les `else` sont inutiles.
|
|
|
|
|
|
|
|
:book: Les constructeurs peuvent aussi soulever des exceptions, dans ce cas **aucun objet n'est créé**.
|
|
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
public class UnexpectedCapacityException extends Exception {}
|
|
|
|
```
|
|
|
|
|
|
|
|
```java
|
|
|
|
/**
|
|
|
|
* Creates a new stack of non negative integers, with given capacity, initially empty.
|
|
|
|
* If given capacity is not strictly positive, it is considered as the default value (DEFAULT_CAPACITY).
|
|
|
|
* @throws UnexpectedCapacityException if capacity is <=0
|
|
|
|
*/
|
|
|
|
public StackOfNonNegativeIntegers(int capacity) throws UnexpectedCapacityException {
|
|
|
|
|
|
|
|
if (capacity <= 0) throw new UnexpectedCapacityException();
|
|
|
|
|
|
|
|
this.capacity = capacity;
|
|
|
|
this.size = 0;
|
|
|
|
this.elements = new int[this.capacity];
|
|
|
|
}
|
|
|
|
``` |
|
|
\ No newline at end of file |