0

Below is my code. When i am running it I am getting Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2 and not my Exception message. Can anyone please explain me what I am doing wrong or why is this happening? Thanks!

Code

public class Main {
    
        public static void main(String[] args) throws Exception{  
            ArrayList a= new ArrayList();
            a.add(10);
            a.add(20);
        
                a.get(3) ;
                throw new IndexOutOfBoundsException("sorry");
            
      }
}
  • 3
    because `a.get(3);` already throws an exception and code execution stops after that. – OH GOD SPIDERS Apr 13 '21 at 13:41
  • What did you expect `a.get(3)` to do? – Ivar Apr 13 '21 at 13:41
  • @OHGODSPIDERS Okay got it thanks. Is there any way i can make it throw the Exception along with my value ```sorry```? – Saurav Shrivastav Apr 13 '21 at 13:45
  • You can add entire block in try & catch. In side catch you can throw your own IndexOutOfBoundsException. – pratap Apr 13 '21 at 13:47
  • Hi @HenryTwist thanks for your suggestion altho I know what causes ArrayIndexOutOfBounds Exception. I am just wondering why cant I handle the exception in my way? Thanks! – Saurav Shrivastav Apr 13 '21 at 13:48
  • 1
    You could catch the IndexOutOfBoundsException and throw a new exception with your custom message: `try { a.get(3); } catch (IndexOutOfBoundsException e) { throw new IndexOutOfBoundsException("sorry"); }` Apart from experimenting and seeing how java exception handling works, this doesn't really have much purpose. – OH GOD SPIDERS Apr 13 '21 at 13:49
  • Yes I am just experimenting with throw and throws keyword. Thanks for your suggestions. – Saurav Shrivastav Apr 13 '21 at 13:51
  • "I am just wondering why cant I handle the exception in my way?" <- The only answer we can give you here is that this is simply not how java works and was designed. You cannot customize (potential) exception messages with the syntax you invented yourself. – OH GOD SPIDERS Apr 13 '21 at 13:51

2 Answers2

2

Your exception is not being thrown because the line before it is throwing the exception and ending execution. The ArrayList already knows to throw an IndexOutOfBoundsException when trying to access an element beyond the size of the List and because it is not caught, your program ends and does not continue to your throw statement.

DLynch
  • 166
  • 1
  • 8
1

Catch this exception using try catch block:

  public static void main(String[] args){
    try {
          ArrayList a = new ArrayList();
          a.add(10);
          a.add(20);
          a.get(3); //element with index 3 doesn't exists
        } catch (IndexOutOfBoundsException e) {
            throw new IndexOutOfBoundsException("sorry");
        }
  }
Sergey Afinogenov
  • 2,137
  • 4
  • 13
  • This is a good answer to the question, but is still an ill-advised approach, since it destroys useful information. Better would be to throw a new (possibly custom) exception with the original as 'cause'. – user15187356 Apr 13 '21 at 13:55
  • Hi Thanks for your answer but i am just experimenting with ```throw``` and ```throws``` and wanted to achieve that with these two only which i got to know is not possible. Altho thanks for your answer. – Saurav Shrivastav Apr 13 '21 at 13:55
  • Instead if using try catch you can check if element with index 3 fits in list and if not then throw exception: `int index=3; if (index<0||index>a.size()-1) throw new IndexOutOfBoundsException("sorry");` @Saurav Shrivastav – Sergey Afinogenov Apr 13 '21 at 14:48
  • Because of IndexOutOfBoundsException is a RuntimeException and is not checked exception, so it doesn't need throws statement @Saurav Shrivastav – Sergey Afinogenov Apr 13 '21 at 14:57