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.