0

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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 3
    See [a similar question on Software Engineering](https://softwareengineering.stackexchange.com/a/284430) – Guy Incognito Mar 12 '23 at 16:10
  • 4
    Apple vs. bananas, but does the Windows Calculator exit automatically if you enter 0 as a divisor? Would you, as a user, be happy if it does? You could just ask your user for a corrected divisor and continue your calculation. Think about user experience. – McPringle Mar 12 '23 at 16:11
  • As the comment under @GuyIncognito answer points out this is known as Single Responsibility Principle. – Aless55 Mar 12 '23 at 16:12
  • A comment by Drew, on the link Guy provided, has a [link](http://en.wikipedia.org/wiki/Single_responsibility_principle) that further explains the concept of "least responsibility" – Jeff Holt Mar 12 '23 at 16:18
  • tldr: it'll make it hard to reason about your program (since you can't rely on things like exceptions, try/finally, return types) and hard to unit test (I don't know of any testing framework that lets you check for an expected process exit). – yshavit Mar 12 '23 at 21:23

1 Answers1

-3

it's not like you should not let the method terminate the program—the caller should decide whether to terminate the program. You can let the method terminate as per your wishes, like terminating the program during an exception, or, as in your case, when it satisfies a condition, you can terminate the program, providing that you use System.exit(0) for safe termination. 0 is actually a status code for the exit() function that implies successful termination. You can use any integer other than 0 as well, but it will be taken as unsuccessful termination.

  • Status - exit(0) - indicates Successful termination
  • Status - exit(-1) - indicates Unsuccessful termination with Exception
  • Status - exit(1) - indicates Unsuccessful termination
Below are some examples that can be used to explain why you are terminating.
System.exit(0) or EXIT_SUCCESS; Simple Success Termination
System.exit(Positive_numbers_other_than_0) or EXIT_FAILURE; ---> Terminating Due to an Exception
System.exit(Negative_numbers) or EXIT_ERROR; ---> Terminating due to Error

Here, error indicates trouble that primarily occurs due to the scarcity of system resources whereas exceptions are the issues that can appear at runtime and compile time.

In Linux and Unix systems, 0 generally denotes successful executions, whereas 1 or above denotes unsuccessful executions.

Sarkar
  • 56
  • 6