Java Program to demonstrate nested try block

Program

public class NestedTryDemo {
    public static void main(String args[]) {
        try {
            int a = Integer.parseInt(args[0]);
            int b = Integer.parseInt(args[1]);
            try {
                int div = a / b;
                System.out.println("Division: " + div);
            } catch (ArithmeticException arEx) {
                System.out.println("Divide by zero exception: " + arEx.getMessage());
            }
        } catch (NumberFormatException nfEx) {
            System.out.println("Incorrect argument type: " + nfEx.getMessage());
        }
    }
}

Output 1

$ java NestedTryDemo 15 3
Division: 5

Output 2

$ java NestedTryDemo 23 a
Incorrect argument type: For input string: "a"

Output 3

$ java NestedTryDemo 6 0
Divide by zero exception: / by zero

Tags: