0

I want to make my own exception. But when I throw my exception the program is close. So how can I run my method 3 times and after that close the program. (my program closed after intArray, but I want to go through dobleArray and charArray)

Here is my Exception:

public class InvalidSubScriptException extends RuntimeException{

    public InvalidSubScriptException(){
        super("Invalid subscript");
    }
}

Here is my code:

import javax.naming.spi.DirStateFactory;


public class GenericTest {

    public static void main(String[] args) {
        Integer[] intArray = {1, 2, 3, 4, 5};
        Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
        Character[] charArray = {'H', 'E', 'L', 'L', 'O'};


        System.out.println("Array integerArray contains:");
        printArray(intArray);
        System.out.println("Array integerArray from possition 1 to 3 contains:");
        printArray(intArray, 1, 3);
        System.out.println("Array integerArray from possition -1 to 30 contains:");
        printArray(intArray, -1, 30);

        System.out.println("Array doubleArray contains:");
        printArray(doubleArray);
        System.out.println("Array doubleArray from possition 1 to 3 contains:");
        printArray(doubleArray, 1, 3);
        System.out.println("Array doubleArray from possition -1 to 30 contains:");
        printArray(intArray, -1, 30);

        System.out.println("Array charArray contains:");
        printArray(charArray);
        System.out.println("Array charArray from possition 1 to 3 contains:");
        printArray(charArray, 1, 3);
        System.out.println("Array charArray from possition -1 to 30 contains:");
        printArray(intArray, -1, 30);
    }

    public static <T> void printArray(T[] inputArray) {
        int counter = 0;
        for (T element : inputArray) {
            System.out.printf("%s ", element);
            counter++;
        }
        System.out.println();
        System.out.println(counter + " element(s) were output");
        counter = 0;
    }

    public static <T> void printArray(T[] inputArray, int lowSubscript, int hightSubscript) throws InvalidSubScriptException {
        int counter = 0;
        if (lowSubscript >= 0 && hightSubscript < inputArray.length) {
            for (int i = lowSubscript; i <= hightSubscript; i++) {
                System.out.printf("%s ", inputArray[i]);
                counter++;
            }
            System.out.println();
            System.out.println(counter + " element(s) were output");
            counter = 0;
        } else {
            throw new InvalidSubScriptException();
        }

    }
}
user3079114
  • 69
  • 1
  • 12

1 Answers1

1

What you are searching for, is how to catch an exception. In Java, you can do this with a try-catch clause.

Since your printArray() is the one that throws the exception, you could surround the calls to that method with a try-catch clause:

try {
    printArray(...);
    // ...
} catch (InvalidSubScriptException e) {
    // what you want to do to handle the exception
}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • I did like you say and in catch block i put System.out.println("ERROR"). When I ran my project I had result: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at GenericTest.printArray(GenericTest.java:54) at GenericTest.main(GenericTest.java:19) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) – user3079114 Mar 23 '14 at 18:32
  • It has nothing to do with the `try-catch`. That exception occurs in your `printArray()` method, and it's because you are accessing to a position larger than the array size. – Christian Tapia Mar 23 '14 at 18:33
  • I found my mistakes, but when I ran the project error message prints every time in the different line. For example, it can print in the output at the 1st line, at the last line, at the 6th line...it doesn't print where it should be print. – user3079114 Mar 23 '14 at 18:53