What will be the output of the following code?

class Test {
    private int i;
    public void setI(int i) {
      this.i = i;
    }

    public int getI() {
      return i;
    }
}

public class Main {
     private Test t;
     public int getSomeInteger() {
         try {
             return t.getI();
         } finally {
             return 0;
         }
     }

     public static void main(String[] args) throws Exception {
         Main m = new Main();
         System.out.println(m.getSomeInteger());
     }
}
Explanation
The output will be 0, since returning anything from the finally block will override the thrown exception.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes