0

I'm trying to understand how I can use java exception handling to return just the exception message when the input is invalid. As far as I understand I have to use return out of try catch, to get this compiled (or in both). But actually I don't want to return anything if the input parameter is invalid.

If I was dealing with strings, there would be null. But that doesn't seem to work with int.

Is there no way doing this?

public class Arrays {

public static int[] intArray = {1,2,3,4,5,60,7,8,9,10};


public static int arrayGet(int[] array, int i){
    try{
        return intArray[i];

    }
    catch (ArrayIndexOutOfBoundsException e){
        System.out.println("Please enter number between 0 and "+i);
    }
}


public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println(arrayGet(intArray,11));
}
}

The code my not make a lot of sense, but I'd like to understand how to deal with the general situation.

4
  • you want to return a number if successful, otherwise string. Correct me if that is not the case. Commented Dec 11, 2016 at 14:42
  • 1
    I think, you should think of escalating the exception to main and catch in main and print. Commented Dec 11, 2016 at 14:48
  • If you want to return the exception message, why catch the exception at all? Just let the caller deal with it. You could wrap it in a checked exception and re-throw too, so you force the caller to deal with it. Commented Dec 11, 2016 at 14:51
  • that makes sense. But, this is a student assignment and we're asked to provide a code for a method, where there's a handling of boundary. Am I right that I can't do this within a method except printing out some message and let the method run into an exception. Commented Dec 11, 2016 at 14:55

3 Answers 3

1

At present your arrayGet is not compilable. If you want to return a String in case of error you can do this in catch block of arrayGet like

} catch (ArrayIndexOutOfBounds e) {
    throw new Exception("my message");
}

And in the main method

try {
    int i = arrayGet(11);
} catch (Exception e) {
    String msg = e.getMessage();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had to change the method declaration a bit public static int arrayGet(int[] array, int i) throws Exception and your code worked fine for me. thx
1

You will have to return some value (which must be an int). With Strings you don't have such a problem because String is not a Standard data type and therefore can be Null.But here you will have to return some value in the catch statement.

Comments

1

The common super type of int and String is Object (after boxing). That means you can return a String or int if you declare the return type as Object:

public static Object arrayGet(int[] array, int i){
    try{
        return intArray[i];    
    }
    catch (ArrayIndexOutOfBoundsException e){
        System.out.println("Please enter number between 0 and "+i);
        return e.getMessage();
    }
}

But the caller will not know which type was returned, so they can only really use it as an Object.

Object o = arrayGet(array, 11);
// o is maybe an int, or maybe a String. But it's definitely an Object.

In this case the arguments of the method are the culprit. A way to let the caller know is by throwing an IllegalArgumentException:

public static int arrayGet(int[] array, int i){
    if(i < 0 || i >= array.length)
        throw new IllegalArgumentException("Please enter number between 0 and " + i);

    return intArray[i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.