Skip to content
Advertisement

How to catch an out of bounds exception in Java and continue?

I am writing a program that converts a prefix expression to a postfix expression. For an expression like ” x-x-ABC+BA ” the program should throw an out of bounds exception, and the program should continue onto the next line (from the input document), I have managed to make it work for one expression like “x-x-ABC+BA”, but where there are two consecutive expression that would yield an out of bounds exception, the program breaks.

public class Main {

    public static void main(String[] args) {

        BufferedReader input = null;
        String prefix_exp;

        try {
            input = new BufferedReader(new FileReader("/Users/khadishaarip/Desktop/requiredinput.txt"));

            try {
                while ((prefix_exp = input.readLine()) != null) {
                    PrefixConverter converter = new PrefixConverter();
                    System.out.println("Postfix : "
                            + converter.preToPost(prefix_exp));
                }
            }catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Too many operators");
                while ((prefix_exp = input.readLine()) != null) {
                    PrefixConverter converter = new PrefixConverter();
                    System.out.println("Postfix : "
                            + converter.preToPost(prefix_exp));
                }
            }


        } catch (IOException error) {
            System.out.println("The file does not exist");
        }
    }
}

Here is how it should output:

Postfix : AB+C-
Postfix : ABC+-
Postfix : AB-C+DEF-+$
Postfix : ABCDE-+$*EF*-
Postfix : ABC+*CBA-+*
Postfix : ABC+/
Too many operators
Postfix : ABC+$CBA-+*
Postfix : AB0+/CBA+-/
Postfix : A^$B*

Here is how it outputs:

Postfix : AB+C-
Postfix : ABC+-
Postfix : AB-C+DEF-+$
Postfix : ABCDE-+$*EF*-
Postfix : ABC+*CBA-+*
Postfix : ABC+/
Too many operators
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 11
    at com.company.Stack.peek(Stack.java:34)
    at com.company.PrefixConverter.preToPost(PrefixConverter.java:55)
    at com.company.Main.main(Main.java:39)

Advertisement

Answer

You catch the exception outside of your while loop – thus, the loop is long over and doesn’t continue when the exception is caught.

Within the catch block, you’re repeating the code reading your input, just now there’s no more catch and retry, so the second occurrence of an exception will end your processing of the input.

Move the exception handling into the first while loop to continue to run the loop once you caught an exception.

Pseudocode, rest left as exercise:

while(read input) {
  try {
    do the conversion
  } catch(Exception) {
    only signal the problem
  }
}

A general opinion on programming style: I don’t think an exception is the proper way to interrupt the flow here, and you might rather think about signalling success any other way. Certainly ArrayIndexOutOfBoundsException feels wrong and is rather an implementation detail of your converter.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement