Ok, so I was just goofing off, creating a class that allowed you to pass in an array,
and pass in the two positions that you want to swap, and then return the array,
it's just a jackass solution, but it is just an experiment anyway. My question
doesn't really relate directly to the array-swapping anyway. After creating the swap
routine, I got to thinking about it, and realized that passing in position parameters
would require error checking, so, having not ever written a custom throw statement,
I decided I would give it a whirl . . . . and it's about half-right, but I am now stuck,
because the class is suppose to return the array, but when I throw the custom error,
I can't seem to return the array!
So the real issue is, if you create a class that returns something, but need to add
error checking, how do you accomplish returning it? The code below throws an
error - "This method must return a result of type int[]"
public class ArraySwapElements { /** swap elements in an array * @param int[] values * @param position1 * @param position2 * @return values[] * */ public static int[] swapElements(int[] values, int position1, int position2) { try{ if(position1 < 0 || position1 > values.length -1 || position2 < 0 || position2 > values.length -1) throw new ArrayException(); int temp = values[position1]; values[position1] = values[position2]; values[position2] = temp; return values; } catch(ArrayException ae){ System.out.println("Parameter position1 or parameter position2 out of bounds."); } } @SuppressWarnings("serial") class ArrayException extends Exception { //Parameterless Constructor public ArrayException() {} //Constructor that accepts a message public ArrayException(String message) { super(message); } } }