I have tried the following code snippet:
private Integer getnumber() {
    Integer i = null;
    try {
        i = new Integer(5);
        return i;
    } catch(Exception e) {
        return 0;
    } finally {
        i = new Integer(7);
    }
}
This method returns 5 and not 7.
Why it returns 5 and not 7?
Thanks in advance.
This happens because the finally block of a try..catch..finally runs after the code within the try..catch has completed (be it successfully or not); in the case of your code this is when the return i has occured.
Because of this behaviour the value of i has already been placed into the return variable of the method before you then assign the new value of 7 to it. The following would work:
private Integer getnumber(){
  Integer i = null;
  try {
    i = new Integer(5);
  }
  catch(Exception e){
    i = new Integer(0);
  }
  finally{
    i = new Integer(7);
  }
  return i;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With