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: ArrayIndex problem

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default ArrayIndex problem

    Hi. I have a problem with an exception error, using an array. The problem is when I pass an object out of the array into my petAnimal method, it doesn't catch the ArrayIndex Exception... ? If I put the try blocks in my main method, it does. Why doesn't it catch it inside the method?

    Here goes

    public static void main(String[] args)
    {
    Animal zoo[] = { new Animal("Cow", 2),
     		     new Animal("Fish", 3),
    		   };
     
    System.out.println("Beginning the program.");
     
    Zookeeper.petAnimal(zoo[25]);
     
    }
     
    ---------
     
    Other class Zookeeper
     
    public class Zookeeper
    {
     public static void petAnimal(animal x)
     { 
      try {
    	System.out.println("Petting the" + x.getAnimalType());
     }
      catch(ArrayIndexOutOfBoundsException e
    {
    System.out.println("That's not an animal silly...");
    }
    finally
    {
    System.out.println("End of zookeeper method");
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: ArrayIndex problem

    when I pass an object out of the array into my petAnimal method, it doesn't catch the ArrayIndex Exception... ? If I put the try blocks in my main method, it does. Why doesn't it catch it inside the method?
    The petAnimal() method will never catch the exception because it is never called. The exception happens before the method is ever called.

    Zookeeper.petAnimal(zoo[25]);

    This line is interpreted as (1) Get the Animal number 25 from the zoo then (2) Send it to petAnimal() method. In Java the arguments are expressions which are always evaluated *before* the method call. That makes sense, if you think about it, because if anything goes wrong with evaluating zoo[25] then what Animal should be passed to the method?

    Bottom line: the exception will occur in main() and must be caught in main() or it will be passed "up" to whoever called main() to deal with. Exceptions go "upwards" in this sense: from the method where the occur to whoever called that method, to whoever called that one, etc. In this case if main() was executed from the command line, no-one will catch the exception and it will be printed at the console.

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

    StuckInJava (March 25th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayIndex problem

    So my only real option then is to handle it within the main? Gotcha. I was hoping to avoid cluttering my main with Try/Catches, but alas. Thanks!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: ArrayIndex problem

    You're welcome.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM