Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDifference Between StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException in Java

Difference Between StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException in Java

An unexpected, unwanted event that disturbed the normal flow of a program is called an Exception.

Most of the time exceptions are caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating in U.S.A. At runtime, if a remote file is not available then we will get RuntimeException saying fileNotFoundException.  If fileNotFoundException occurs we can provide the local file to the program to read and continue the rest of the program normally.

There are mainly two types of exception in java as follows:

1.Checked Exception :

The exception which is checked by the compiler for the smooth execution of the program at runtime is called a checked exception. In our program if there is a chance of rising checked exception then compulsory we should handle that checked exception (either by try-catch or throws keyword) otherwise we will get compile-time error;

Examples of checked exceptions are ClassNotFoundException, IOException, SQLException, etc.

2. Unchecked Exception:

The exceptions which are not checked by compiler, whether programmer handling or not such type of exception are called an unchecked exception.

Examples of unchecked Exceptions are ArithmeticException, ArrayStoreException etc.

Whether the exception is checked or unchecked every exception occurs at run time only if there is no chance of occurring any exception at compile time.

ArrayIndexOutOfBoundException: ArrayIndexOutOfBoundException is the child class of RuntimeException and hence it is an unchecked exception. This exception is rise automatically by JVM whenever we used a negative value or a value greater than or equal to the length of the given array as an index of an array we will get this exception.

Example:  

Java




// Java program to demonstrate the
// ArrayIndexOutOfBoundException
 
// import the required package
import java.io.*;
import java.lang.*;
import java.util.*;
 
// driver class
class GFG {
 
    // main method
    public static void main(String[] args)
    {
 
        // declaring and initializing an array of length 4
        int[] x = { 1, 2, 3, 4 };
 
        // accessing the element at 0 index
        System.out.println(x[0]);
 
        // accessing an index which is greater than the
        // length of array
        System.out.println(x[10]);
 
        // accessing a negative index
        System.out.println(x[-1]);
    }
}


Output:

StringIndexOutOfBoundException: StringIndexOutOfBoundException is the child class of RuntimeException and hence it is an unchecked exception. This exception is rise automatically by JVM whenever in any string class method we use the index value which is either negative or greater than or equal to the length of the given string.

Example:

Java




// Java program to demonstrate
// StringIndexOutOfBoundException
 
// import required packages
import java.io.*;
import java.util.*;
 
// driver class
class GFG {
 
    // main class
    public static void main(String[] args)
    {
 
        // declaring a string
        String s = "GEEKSFORGEEKS";
 
        // accessing the second character of the given
        // string using charAt() method
        System.out.println(s.charAt(1));
 
        // now using an index greater than the length of the
        // string
        System.out.println(s.charAt(30));
    }
}


Output:

                ArrayIndexOutOfBoundException                      StringIndexOutOfoundExcetion
It is thrown to indicate that an array has been accessed with an illegal index(i.e index value is either negative or greater than or equal to the length of an array). It is thrown by the method of string class to indicate that an index value used in the string method is either negative or greater than or equal to the length of the given string.
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments