Multi-catch

Lab Java Intermediate 20 points

The array is {10, 20, 30}. Read tokens; for each, print arr[index]. Catch NumberFormatException and ArrayIndexOutOfBoundsException in one catch block and print error: SIMPLE_CLASS_NAME.

Input

n, then n tokens.

Output

n lines.

Example

Input

4
1
x
7
0

Output

20
error: NumberFormatException
error: ArrayIndexOutOfBoundsException
10
Sample input available Sample output available
Editor
Java

Hints

catch (TypeA | TypeB e) handles both types (Java 7+).

Solution explanation

Multi-catch avoids duplicating identical handler code.

Show solution
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30};
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            String token = sc.next();
            try {
                System.out.println(arr[Integer.parseInt(token)]);
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                System.out.println("error: " + e.getClass().getSimpleName());
            }
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD