Search code examples
javaexceptiontry-catchthrowthrows

Why cant I use a method that throws in a catch block? Why use try/catch throw/throws in general?


This is the example. Taschenrechner just means calculator.

This is the main class:

        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        try {
            Taschenrechner.divide(num1,num2);
        }
        catch (Exception e) {
            System.out.println("Wrong entry repeat your entry");

            num1 = sc.nextInt();
            num2 = sc.nextInt();

            try {
                Taschenrechner.divide(num1,num2);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }

    }
}

This is the Taschenrechner class with the class method:

    public static void divide(int userZahl1, int userZahl2) throws Exception {
        if(userZahl2 == 0) {
            throw new Exception();
        }
        else {
            System.out.println("Ergebnis : " + (userZahl1/userZahl2));
        }
    }
}

What I don't get is why I have to wrap the second Taschenrechner.divide() method, within the catch block, in another try/catch block?

In general I don't quite understand why I use the throw/throws and try and catch in Java?

Is it really the more efficient way to handle errors instead of if clauses like

 if(userEntry == 0 ){ System.out.println("Wrong entry. Try again!"); } ? 

Solution

  • First of all, it's important to understand that there are 2 types of exceptions:

    • Checked exceptions
    • Unchecked exceptions

    Where the later one is all exceptions in which are inherit from RuntimeException and you shouldn't declare the throws for them in your method signature.

    According to the book Hardcore Java, RuntimeException should be thrown only when the error is a programmatic error (meaning the programmer made a mistake while writing the code, and passed some invalid input for example).

    In general, RuntimeException should be thrown only when the program cannot recover from the error that happened. If we can actually solve this error, we will wrap it with a try-catch block and recover from it.

    As for your question specifically, the first call to Taschenrechner.divide(num1,num2); might throw checked exception (from type Exception) and therefore whoever is calling this function must do one of the following when calling divide()

    • wrap the call with try-catch block
    • declare throws on your method signature and let whoever is calling this method handles the exception

    As for the question about efficiency - remember that sometimes you write code for someone else to use. let's say that this person passing wrong input into your code - how can you communicate to this person that something bad happened without exceptions? (or in old languages error code...)

    You can learn checked/unchecked exceptions here for an example: https://howtodoinjava.com/java/exception-handling/checked-vs-unchecked-exceptions-in-java/