Home > Net >  What is the return type of a Java exception?
What is the return type of a Java exception?

Time:09-18

I'm writing a Calculator exercise that takes an array of int for addition, subtraction, division and multiplication. I need to check in every method that the array is not null. Instead of repeating myself, can I write the throwException method once and then just call it in my method? e.g. for addition

public int addAll(int[] integers) {
    throwExceptionForNullArray();
    int sumOfAllIntegers = 0;
    for (int integer : integers) {
        sumOfAllIntegers  = integer;
    }
    return  sumOfAllIntegers;
}

public throwExceptionForNullArray (int[] integers){
    if (integers == null){
        throw new IllegalArgumentException("Empty array is not allowed");
    }
}

But Java requires a return type in a method, do you have any ideas that can make the throwExceptionForNullArray method work? What should be the return type? Thank you

CodePudding user response:

What is the return type of a Java exception?

That's like asking: "What is the colour of ennui?" - the question doesn't make sense.

Let me put it differently. Imagine this hypothetical method:

public int hello() {
  while (true) {
    // just loop forever and ever and ever.
  }
}

The above will... actually compile. Try it!

That's a bit odd - this method is declared to return an int, and yet, the method contains no return statement at all. And yet this compiles fine. That's because it is a bit wonky in how it 'works'. It never actually exits, so there's no violation of the rules here. A return type of int means: If this method returns normally, it must do so by returning an int value.

Note, if it returns normally. There is no rule stating that all methods must necessarily always return normally, or that it could ever even return normally. The above method cannot return normally, for example.

A "normal" return occurs when your method hits a return statement, or just gets to the closing brace.

A throw statement is an abnormal condition. A method that exits by way of throwing does not return anything. It got out by throwing. That doesn't mean it "returned" the exception. It means it "threw" the exception.

A method that cannot possibly return normally (and if all ways through the method end in a throw statement, it cannot possibly return normally) can pick whatever it wants for a return type. It just won't matter. Anything will do. For a method whose stated purpose is:

  • If the inputs are valid, do absolutely nothing
  • Otherwise, throw an exception highlighting what's invalid about the input

The usual return type is void. So, do that:

public void checkInput(int[] integers) {
    if (integers == null) {
        throw new NullPointerException("integers");
    }
}

A few notes:

  • Your message was dead wrong. null does not mean "empty", a very important distinction. new int[0] is an empty array. null is a null pointer. Don't conflate the two. It's possible to have a method that doesn't like an input of an empty array. In that case, the correct act to take is something like:
if (integers.length == 0) throw new IllegalArgumentException("Empty 'integers' array not allowed");

It's more appropriate to use NullPointerException for params being null when as a precondition they aren't supposed to be.

  • If you're dereferencing the parameter, there's really no need to do this. The JVM itself will throw the NPE for you and even name it assuming you're using a modern JVM. You dereference integers (for (int i : integers) will do that), so you can remove it all and get the same result, namely: Calling that method with null will throw an NPE whose message includes the text 'integers'.
  • Naming a method 'throwExceptionFor' is a bit too on the nose, it describes too much how it works and not enough what it is supposed to do. checkInput describes the why/what instead of the how, that's usually the better way to name methods.
  • Related