java interview question exception handling

idiot
Jun 8, 2024

--

Photo by Florian Olivo on Unsplash

what will be output of below program

public class Test {
public static void main(String[] args) {
try {
System.out.println("A");
m1();
} catch (Throwable e) {
System.out.println("B");
} finally {
System.out.println("C");
}
}

public static void m1() {
throw new Error();
}
}

output

A
B
C

what will be output of below program

public class Test {
public static void main(String[] args) {
try {
System.out.println("A");
m1();
} finally {
System.out.println("B");
}
System.out.println("C");
}

public static void m1() {
throw new RuntimeException();
}
}

output

A
B

what will be output of below program

public class Test {
public static void main(String[] args) {
try {
System.out.println("A");
m1();
} catch (Exception e) {
System.out.println("B");
} finally {
System.out.println("C");
}
System.out.println("D");
}

public static void m1() {
// No exception thrown
}
}

output

A
C
D

--

--