Sobes.tech
Junior — Senior

Analysis of try‑catch‑finally block behavior

livecode

Task condition

What will be the output of the program and why? Which parts of the code will be executed? Why will the catch (Exception e) block not be called? Is the finally block executed when an exception occurs?

public class ExceptionQuiz {
    public static void main(String[] args) {
        try {
            int i = 1 / 0;
            System.out.println("1");
        } catch (ArithmeticException e) {
            System.out.println("2");
            throw new RuntimeException(e);
        } catch (Exception e) {
            System.out.println("4");
        } finally {
            System.out.println("5");
        }
    }
}