-1

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!"); } ? 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Fabian Andiel
  • 321
  • 1
  • 13
  • 1
    "Is it really the more efficent way to handle errors instead of if clauses?" In this case, I don't think so. I think this particular example is quite contrived. Its sole purpose seems to be to practice using `throws`. In the real world there are many cases where you can't check for whether an exception will occur before hand. – Sweeper Feb 08 '20 at 15:08
  • In a scenario like this I would handle the input separately in a loop that continues until 2 correct values are entered (or user somehow gives up) then you might throw an exception in your divide method as well since taking input and doing the calculations are two independent tasks – Joakim Danielson Feb 08 '20 at 15:08
  • I for myself, would do both: 1. check user input (since it always can be wrong) and request new entry (in a loop); 2. check arguments passed to variable and throw an Exception (for the **exceptional** case that the developer did not do the right thing) – user85421 Feb 08 '20 at 15:17

2 Answers2

1

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/

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Yonatan Karp-Rudin
  • 1,056
  • 8
  • 24
0

What if userZahl2 is null instead of 0. It will raise an exception again. Yes, In your case, you can easily manage the things but sometimes things are much complicated than how it looks. So while writing a program, if we think that certain statements in a program can throw an exception, we mostly enclosed them in the try block to handle that exception.

emredmrcn
  • 144
  • 1
  • 5