I was studying exceptions from java reference textbook and I found this sentence stating:
You should not let the method terminate the program—the caller should decide whether to terminate the program.
the sentence was pointing to the following code:
class QuotientWithMethod {
public static int quotient(int number1, int number2) {
if (number2 == 0) {
System.out.println("Divisor cannot be zero");
System.exit(1); //< ---------- HERE
}
return number1 / number2;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
int result = quotient(number1, number2);
System.out.println(number1 + " / " + number2 + " is " + result);
}
}
what's meant by you shouldn't let a method terninate your program, is it dangerous?
I searched about using System.exit() if it's safe to use it in java or not, and I found that it's better than using Runtime.exit() but it can throw unexpected errors.