finally execution order

    public class Test {
        public int getX() {
            int x;
            try {
                x = 7;
                return x;
            } catch (Exception e) {
                x = 8;
                return x;
            } finally {
                x = 9;
                // 接下来看注释与不注释的区别
                return x;
            }
        }
    }

Write java may get the right result is not difficult, but our goal is to know why this happened?

My question is, in point of fact, finally return if you do not add the line, why return 7, we can see through the debugger execution order is assigned 7, and then copy the 9, then return. Why finally becomes seven.

The reason we can actually get the problem by looking at the byte code file

Bytecode

There are return

No return

tips:

  1. Abnormal also a variable
  2. After completion finally performs the try block in which no abnormality, but the return value

link

https://blog.csdn.net/u012070360/article/details/81624854

Guess you like

Origin www.cnblogs.com/colin-xun/p/11772787.html