user3079114
user3079114

Reputation: 69

Custom Exception (RuntimeException)

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();
        }

    }
}

Upvotes: 0

Views: 779

Answers (1)

Christian Tapia
Christian Tapia

Reputation: 34146

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
}

Upvotes: 1

Related Questions