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 3 of 3

Thread: Array Out Of Bounds Exception Question

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Out Of Bounds Exception Question

    I Have a question which I'm hoping someone can help me with as I don't know how to write this exception so here it is

    I want Develop a new Exception Class called “Invalid Index Exception” which will inform the user of an attempt to access an invalid index in an array.

    Then I want to give a Java method only called “get Value” which takes 2 parameters, an integer (I) representing the subscript and an integer Array (A), and returns the element at position “I” in the Array “A”. This method should throw an Invalid Index Exception if “I” is not a valid index in the Array “A”.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array Out Of Bounds Exception Question

    You can reinvent the wheel if you want to, but you do not have to.
    But if you want to you can extend Exception or a subclass, like the link above and provide your own functionality

    --- Update ---

    Thread moved from "Whats wrong with my code?"

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Array Out Of Bounds Exception Question

    Olliedee,
    You will need to write one user-defined exception class that can extend ArrayIndexOutOfBoundsException.
    Still, you will need to catch ArrayIndexOutOfBoundsException in your source code.
    Then in the catch block you can re-throw your user-defined exception.
    But then how to extract information about array name and index. That I don't know as of now. And I don't know if that is possible also.
    But you can get the cause. I don't know how far "cause" fulfills your requirement.
    Let me know once your dream gets fulfilled. I may be learning new thing.

    Syed.

Similar Threads

  1. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 01:44 AM
  2. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  3. Can't Fix Array Out of Bounds
    By Apocalypse in forum Collections and Generics
    Replies: 5
    Last Post: October 31st, 2011, 07:37 AM
  4. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  5. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 08:05 AM

Tags for this Thread