Results 1 to 4 of 4
  1. #1
    langdatyagi is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default what is "ArrayIndexOutOfBoundsException" during exception handling?

    // File Name : ExcepTest.java
    import java.io.*;
    public class ExcepTest{

    public static void main(String args[]){
    try{
    int a[] = new int[2];
    System.out.println("Access element three :" + a[3]);
    }catch(ArrayIndexOutOfBoundsException e){
    System.out.println("Exception thrown :" + e);
    }
    System.out.println("Out of the block");
    }
    }

    _____________________________----

    Is it a name on an exception?
    I can't understand the line catch(ArrayIndexOutOfBoundsException e). I understand that we are trying to access 4th element a[3] of array which doesn't exist. So it throws an exception. So is ArrayIndexOutOfBoundsException a type of exception? And what is e?

    Please help

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: what is "ArrayIndexOutOfBoundsException" during exception handling?

    is ArrayIndexOutOfBoundsException a type of exception?
    Yes.
    The API doc is one place to look up the meaning of an exception. They are all classes that are documented in the API doc.
    And what is e?
    e is a variable for the object that is passed to the catch block.

    See the tutorial for more discussions: http://docs.oracle.com/javase/tutori...ons/index.html
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    langdatyagi is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: what is "ArrayIndexOutOfBoundsException" during exception handling?

    Quote Originally Posted by Norm View Post
    Yes.
    The API doc is one place to look up the meaning of an exception. They are all classes that are documented in the API doc.

    e is a variable for the object that is passed to the catch block.

    See the tutorial for more discussions: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
    ohh thanks now i am getting a little bit what that means.
    so try block converts exception to object and that object is passed to nearest catch block. The output of the above program is

    Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
    Out of the block


    how do this object 'e' work in this line? "System.out.println("Exception thrown :" + e)"

    how object e prints "java.lang.ArrayIndexOutOfBoundsException: 3"

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: what is "ArrayIndexOutOfBoundsException" during exception handling?

    how do this object 'e' work in this line?
    The compiler creates a call to the e object's toString() method which returns the String that you see printed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 05-26-2013, 07:42 AM
  2. Replies: 6
    Last Post: 04-27-2012, 12:37 AM
  3. Replies: 4
    Last Post: 04-23-2012, 10:13 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •