Try/Catch IndexOutOfBoundsException Tutorial

This tutorial builds on concepts from my Exception Handling: Try and Catch Tutorial. One of the things that I emphasized in that tutorial is the importance of trial and error when it comes to gaining experience in exception handling. The IndexOutOfBoundsException is a very common exception class. It has two subclasses, ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException. The documentation for most of the methods in the String class claim that they throw the IndexOutOfBoundsException, in reality they really throw StringIndexOutOfBoundsException. I'll explain why I find this to be a bit of an annoyance, and why the documentation can get away with it.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md TryCatchIOOBE
C:\Java>cd TryCatchIOOBE
C:\Java\TryCatchIOOBE>Notepad TryCatchIOOBE.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class TryCatchIOOBE {
    public static void main(String args[]) {
        String hello = "Hello World!";

        char test = hello.charAt(4);
        System.out.println(test);
        test = hello.charAt(27); // get the 27th character;

        /*try {
            char test = hello.charAt(4);
            System.out.println(test);
            test = hello.charAt(27); // get the 27th character;
            System.out.println(test);            
        } catch (IndexOutOfBoundsException e) {
            String s = "Only in this code block.";
            System.out.println("Oops, we can\'t get the 27th character in the string Hello World.");
        }*/

        //test = 'A'; // variable scope applies to try blocks
        //s = "Scope lesson also"; // variable scope applies to catch blocks
    
        System.out.println("\n-------");

        int array[] = { 12, 24, 36 };
        
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);
        System.out.println(array[array.length]);
        
        /*try {
            System.out.println(array[array.length]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Oops, we tried to access an element at an index that doesn't exist!");
        }*/
    }
}

Now switch back to the command prompt (CMD) and type in javac TryCatchIOOBE.java and press Enter.
Now type in java TryCatchIOOBE and press Enter.


C:\Java\TryCatchIOOBE>javac TryCatchIOOBE.java
C:\Java\TryCatchIOOBE>java TryCatchIOOBE
o
Oops, we can't get the 27th character in the string Hello World.

-------
12
24
36
Oops, we tried to access an element at an index that doesn't exist!


Final thoughts

It is perfectly valid to catch exceptions that are higher up the hierarchy, although it is not recommended because you lose the ability to know precisely what the specific issue was that triggered the exception.


Tutorials