Why does a return in `finally` override `try`?

后端 未结 10 2014
栀梦
栀梦 2020-12-12 15:22

How does a return statement inside a try/catch block work?

function example() {
    try {
        return true;
    }
    finally {
        return false;
             


        
10条回答
  •  庸人自扰
    2020-12-12 15:50

    When you use finally, any code within that block fires before the method exits. Because you're using a return in the finally block, it calls return false and overrides the previous return true in the try block.

    (Terminology might not be quite right.)

提交回复
热议问题