• Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • #Deals
  • #News
  • #WiKi
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
ERROR

WHAT ARE IndexOutOfBoundsException AND HOW TO AVOID THEM?

WHAT ARE IndexOutOfBoundsException AND HOW TO AVOID THEM?

Moving along through the detailed Java Exception Handling series we’ve been working on, today we’ll be going over the IndexOutOfBoundsException. The IndexOutOfBoundsException is thrown when attempting to access an invalid index within a collection, such as an array, vector, string, and so forth. It can also be implemented within custom classes to indicate invalid access was attempted for a collection. All Java errors implement the java.lang.Throwable interface, or are extended from another inherited class therein. The full exception hierarchy of this error is:

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
IndexOutOfBoundsException

public class IndexOutOfBoundsException extends RuntimeException
It comes with two subclasses used very often in coding with arrays and strings.

1. ArrayIndexOutOfBoundsException

2. StringIndexOutOfBoundsException

Following two programs discuss the above two exceptions.

1. ArrayIndexOutOfBoundsException
We know exceptions are errors that creep into the execution at the runtime and if not handled properly, the execution will be terminated abruptly. The cause of exceptions is the actual user (for whom the product is delivered) and thrown when he inputs wrong values which the JRE (Java Runtime Environment) is unable interpret or execute.

One such runtime problem, a user, can do is giving wrong index number which does not exist in the array at all. If such index number is given, what output shall to be given by the system. System (known in Java as JRE; JRE is part of JVM) reacts to it as an unchecked exception with “ArrayIndexOutOfBoundsException”.

public class IndexDemo
{
public static void main(String args[])
{
int marks[] = { 40, 50, 60 };

try
{
System.out.println(marks[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(“Wrong index number, please enter correct number. ” + e);
}
}
}
In the above code, the input for marks[] array can be taken from keyboard. For simplicity, it is taken directly as 3. The 3rd index number does not exist in the marks array; the index number are only 0, 1 and 2. The system cannot access the element and give any output; consequently throws ArrayIndexOutOfBoundsException and is caught in catch block.

2. StringIndexOutOfBoundsException
Just like ArrayIndexOutOfBoundsException exception works on array, StringIndexOutOfBoundsException works on strings. This is an unchecked exception thrown by the system when the user accesses a character that does not exist in the string (earlier it is an array). This exception is thrown by charAt() method of String class.

public class IndexDemo
{
public static void main(String args[])
{
String str = “hello”;
try
{
System.out.println(str.charAt(5));
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println(“Wrong character index number, please enter correct number. ” + e);
}
}}

WHAT ARE IndexOutOfBoundsException AND HOW TO AVOID THEM?

In the above code, the charAt(int) method returns the character present at the index value passed as parameter. It is passed as 5 where the characters lie 0 to 4. This is shown by the JVM by throwing StringIndexOutOfBoundsException exception. It is handled in catch block.

  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 1936 Views
Tags | error
AUTHOR
Ranjan

I m Ranjan and Sharing my years of experience in Software Development. Love to code in Mobile apps (IOS, Android, Power Apps, Xamarin, Flutter), Machine Learning ( Beginner ), Dot Net, Databases ( SQL Server, MySql, SQLite), WordPress, Cloud Computing ( AWS, Azure, Google, MongoDB) and many more as required on project-specific. Besides this love to travel and cook.

You Might Also Like

thumb overflow error

What is Overflow Error in Software Programming

October 4, 2019
thumb null point error

What are Null Pointer Exceptions (java.lang.NullPointerException) and how to avoid them?

October 29, 2018
Next Post
Previous Post

Subscribe for updates

Join 5,736 other subscribers

whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

Latest Posts

  • Frameworks of IOS
    Frameworks of IOS – Part ( I )
  • NSFileManager or NSPathUtilities
    NSFileManager or NSPathUtilities in Objective-C
  • Passing data between view controllers in Objective-C
    Passing data between view controllers in Objective-C
  • structures-classes-enum
    Structures and Classes in swift !
  • control-system-swift
    Control Flow in Swift
  • swift-concurrency-await
    Concurrency in Swift
  • time-complexity-dsa
    The Term Time Complexity in DSA
  • objective-c-datatypes1
    Objective-C Data Types
  • Convert-jpeg-word
    Convert JPG to Word – Tips You Should Try!
  • objective-c-control-statements2
    Objective-C control statements and loops !

Quick Links

  • #about
  • #myapps
  • #contact
  • #privacy

Other Websites

  • #myQuestions
  • #myBhojanalaya
  • #gadgetFacts
  • #ifscCodesDB

Tag Cloud

Android Android Studio API APP Programming Apps ARC asp.net blogging Browser Config CSS DATABASE DFD error Features GUI HTML HTML5 IDE IIS installation Interview Questions IOS iPhone javascript Mac objective-c OneDrive OS Programming quicktips SDK SEO Settings SMO SQL swift swiftUI Teams Tips & Tricks Tools UI Web Wordpress Xcode

©mycodetips.com