A
A
Alex vak2017-06-10 16:06:40
Java
Alex vak, 2017-06-10 16:06:40

How to properly handle exceptions in Java?

Hello dear toasters. I have questions about exception handling in Java. There is a Counter
class :

package com.vogella.ide.counter.util;

public class Counter {
  public int count (int x){
    // TODO check that x > 0 and <= 255
    // if not throw a new RuntimeException
    // Example for a RuntimeException:

    // throw new RuntimeException("x should be between 1 and 255");

    // TODO calculate the numbers from 1 to x
    // for example if x is 5, calculate
    // 1 + 2 + 3 + 4 + 5


    // TODO return your calculated value
    // instead of 0
    return 0;
  }
}

Which, after completion, will receive x inside itself , check x for falling into the range, if x does not fall into the range, then throw a RuntimeException with the inscription "x should be between 1 and 255" printed on the screen, and if x falls into the range, then it is calculated the sum from 1 to x, and the value of the sum is returned to the caller of the method. Am I correct in understanding how this class works? In this class, the return 0 line is used as a stub, and after finishing the class, will it need to be removed? What is the best way to implement exceptions in this class? There is also a Tester
class that is related to the Counter class :
package com.vogella.ide.counter.main;

import com.vogella.ide.counter.util.Counter;

public class Tester {

  public static void main(String[] args) {
    Counter counter = new Counter();
    int result = counter.count(5);
    if (result == 15) {
      System.out.println("Correct");
    } else {
      System.out.println("False");
    }
    try {
      counter.count(256);
    } catch (RuntimeException e) {
      System.out.println("Works as exepected");
    }
  }

}

As I understand the Tester class tests the Counter class, is the Tester class implemented correctly ?
At the top of the Tester class are these lines: Finish the implementation for the Tester class based on the following code. The Tester class checks for an example value but the method should work for different input values.
It turns out that the Tester class is not written completely correctly (it is a blank)?
What does the line mean: The Tester class checks for an example value but the method should work for different input values? What method is meant here? Sum number ( result == 15) in the Tester class should be calculated automatically or set by the user?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-06-10
@zagayevskiy

Yes, return will be superfluous. In such cases, they usually throw
throw new IllegalArgumentException("x should be between 1 and 255");
About the rest of the implementation, you need to ask the authors of such a task. Testing by counting 15 automatically is stupid, tests shouldn't repeat the implementation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question