Java's strange behavior while returning from finally block

后端 未结 6 2163
梦谈多话
梦谈多话 2021-01-02 03:26

Try this piece of code. Why does getValueB() return 1 instead of 2? After all, the increment() function is getting called twice.

    public class ReturningF         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 04:04

    You do not have an explicit return in the second example. In this case, it will return the value within the try block. It makes intuitive sense because Java has already executed the code inside the try block. It would not the executed that block again after executing the finally block.

提交回复
热议问题