überprüfte Ausnahmen für
wiederherstellbare Bedingungen und Laufzeitausnahmen
für Programmierfehler verwenden
(Punkt 58 in der 2. Auflage)
Mal sehen, ob ich das richtig verstehe.
Hier ist mein Verständnis einer geprüften Ausnahme:
Code: Select all
try{
String userInput = //read in user input
Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
id = 0; //recover the situation by setting the id to 0
}
2. Ist RuntimeException eine ungeprüfte Ausnahme?
Hier ist mein Verständnis einer ungeprüften Ausnahme:
Code: Select all
try{
File file = new File("my/file/path");
FileInputStream fis = new FileInputStream(file);
}catch(FileNotFoundException e){
//3. What should I do here?
//Should I "throw new FileNotFoundException("File not found");"?
//Should I log?
//Or should I System.exit(0);?
}
Code: Select all
try{
String filePath = //read in from user input file path
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
}catch(FileNotFoundException e){
//Kindly prompt the user an error message
//Somehow ask the user to re-enter the file path.
}
Code: Select all
public void someMethod throws Exception{
}
6. Soll ich die genaue Ausnahme aufblasen oder sie mit „Exception“ maskieren?
Unten sind meine Messwerte
Wann sollte ich in Java eine aktivierte Ausnahme erstellen und wann sollte es eine Laufzeitausnahme sein?
Wann sollten aktivierte und nicht aktivierte Ausnahmen ausgewählt werden
Mobile version