Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Experiment gone wrong . . . exception in class

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    25
    My Mood
    Devilish
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Experiment gone wrong . . . exception in class

    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);
    	      }
    	 }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Experiment gone wrong . . . exception in class

    The usual way to get the behaviour you want is to have your swapElements throw the exception back to the caller and make them handle it and not your swapElements method. Contrived example:
    public Foo someMethod(int x, int y) throws SomeException {
        if( x > y) {
            throw new SomeException("x cannot be greater then y");
        }
        return new Foo();
    }
     
    public void blah() {
        try {
            Foo f = someMethod(10,7);
        } catch(SomeException e) {
            e.printStackTrace();
        }
    }
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    Skywola (November 14th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    25
    My Mood
    Devilish
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Experiment gone wrong . . . exception in class

    That makes sense . . . . I'll give it a whirl. Thanks.

    Later note:
    Just in case anyone wants to see what I cooked up from this:

    public static int[] swapElements(int[] values, int position1, int position2) throws ArrayIndexOutOfBoundsException{
    		if(position1 < 0  || position1 >= values.length || position2 < 0  || position2 >= values.length){
    			throw new ArrayIndexOutOfBoundsException();
    		}
    		int temp = values[position1];
    		values[position1] = values[position2];
    		values[position2] = temp;
    		return values;
    		}
    	}

    Two big changes:
    1. Did away with the custom error; ArrayIndexOutOfBoundsException already exists, why not use it.
    2. Removed the magic numbers, -1 from the code
    position1 >= values.length
    by inserting an equals sign instead.

  5. #4
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Experiment gone wrong . . . exception in class

    You could've also moved the return statement outside of the try/catch block

Similar Threads

  1. Code Experiment - Why the change?
    By Skywola in forum Java Theory & Questions
    Replies: 3
    Last Post: October 19th, 2013, 02:45 PM
  2. Trying to repeat experiment 100 times but results vary for elapsed time
    By IHeartProgramming in forum Loops & Control Statements
    Replies: 3
    Last Post: October 22nd, 2012, 01:36 PM
  3. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  4. What is Wrong With My Exception Class Code?
    By Allicat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2011, 10:09 AM

Tags for this Thread