What will be printed when the following program is compiled and run?
public class Main{ public static void main (String args []) throws Exception{ try{ /*from w w w . ja v a 2 s . co m*/ method1 (); System.out.println ("A"); } finally{ System.out.println ("B"); } System.out.println ("C"); } public static void method1 () throws Exception { throw new Exception (); } }
Select 1 option
Correct Option is : C
As there is no catch block the exception will not be handled and the main () method will propagate the exception.
So println ("C"); will also not be executed.
'finally' block is always executed (even if there is a return in try but not if there is System.exit()
) so println ("B") is executed.