I am trying to understand and use exceptions.
why use finally? Instead of finally I could also do a catch(Exception e){}. This way my program never crashes and the code after the catch is always executed. Additionally I could add an if/else after the try and catch to check if an exception was thrown. This way my program would never crash and the if/else creates a standard behavior.
E.g. pseudo-code:
public void enterAndPrintANumber() {
boolean exception = false;
int number = 0;
try {
System.out.println("Please enter a Number")
number = BufferedReader.readLine();
} catch(NumberFormatException e) {
System.out.println("Please enter a number")
}
catch(Exception e) {
System.out.println("An error has occurred")
exception = true;
}
if(exception) {
System.out.println("Action will be restarted")
this.enterAndPrintANumber();
} else {
System.out.println("Your Number is "+number)
}
}