Java Exception Handling with getNum012 Method

Java Bytes #5 What will be the output of the following code? class Main {   private static int getNum012(int num) {     try {       if(num == 1) {         return 1;       }       if(num == 0) {         num = num / 0;         return 0;       }     } catch(ArithmeticException e) {       return 0;     } finally {       return 2;     }   }       public static void main(String[] args) {     System.out.println(getNum012(0));     System.out.println(getNum012(1));     System.out.println(getNum012(2));   } } ---------------------------------------------------------------- Java is like my girlfriend.  I have misunderstood her many times.  Still I try to understand her a lot.  Please feel free to correct me if I have misunderstood her. #javaBytes  #javaIsLove  #javaInterviewPrep

Output: 2 2 2 Everytime 2 is returned because the finally block's return statement overwrites the return statement from try or catch block

Like
Reply

To view or add a comment, sign in

Explore content categories