What is the output of the following application?
package mypkg; /* w w w . j av a 2s . com*/ public class Main { public static void main(String[] dribble) { try { System.out.print(1); throw new ClassCastException(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.print(2); } catch (Throwable ex) { System.out.print(3); } finally { System.out.print(4); } System.out.print(5); } }
A.
The code compiles and runs without issues, so Options C and D are incorrect.
The try block throws a ClassCastException.
Since ClassCastException is not a subclass of ArrayIndexOutOfBoundsException, the first catch block is skipped.
For the second catch block, ClassCastException is a subclass of Throwable, so that block is executed.
Then, the finally block is executed and then control returns to the main()
method with no exception being thrown.
The result is that 1345 is printed, making Option A the correct answer.