Appliquer la notion
Avec cet exercice, mettez en application la notion d'exceptions personnalisées.
Question
Indiquez les éléments manquants pour définir une nouvelle exception de type InsufficientFundsException.
1
import java.io.IOException;
2
3
import java.io.*;
4
5
public class _________________________ extends _________ {
6
private double amount;
7
8
public InsufficientFundsException(double amount) {
9
this.amount = amount;
10
}
11
12
public double getAmount() {
13
return amount;
14
}
15
}
Indice
Si vous souhaitez écrire une exception vérifiée, vous devez étendre la classe Exception.
Si vous souhaitez écrire une exception d'exécution, vous devez étendre la classe RuntimeException.
Solution
1
import java.io.*;
2
3
public class InsufficientFundsException extends Exception {
4
private double amount;
5
6
public InsufficientFundsException(double amount) {
7
this.amount = amount;
8
}
9
10
public double getAmount() {
11
return amount;
12
}
13
}