• Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
  • Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk
  • #Advertise

MyCodeTips mycodetips-newlogocopy1

  • Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • 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.

Liked it? Take a second to support Ranjan on Patreon!
become a patron button
  • 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)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 2898 Views
Tags | error
AUTHOR
Ranjan

Namaste, My name is Ranjan, I am a graduate from NIT Rourkela. This website is basically about of what i learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures, its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

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

Support us

mycodetips mycodetips
Follow us @ LinkedIn 2850+

Subscribe for updates

Join 8,213 other subscribers

Latest Posts

  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
  • r-language
    How can R language be used for data analysis?
  • wordpress-coding-blog
    Guide To WordPress Coding Standards
  • YT-Featured-Algorithm
    What is Algorithm?
  • 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
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #Guestpost
  • #myQuestions

Android Android Studio API APP Programming Apps blogging CSS DATABASE dsa Features HTML HTML5 installation Interview Questions IOS iPhone javascript Mac objective-c OS Programming quicktips SDK SEO SQL swift Tips & Tricks Tools UI Web Wordpress Xcode

  • SOLID Principles of Software Design
  • Latest features in IOS 16
  • How can R language be used for data analysis?
  • Guide To WordPress Coding Standards
  • What is Algorithm?

©mycodetips.com