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

后端 未结 10 2023
栀梦
栀梦 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:51

    What about this?

    doubleReturn();
    
    function doubleReturn() {
      let sex = 'boy';
    
      try {
        return sex;
    
        console.log('this never gets called...');
      } catch (e) {} finally {
        sex = 'girl'; 
    
        alert(sex);
      }
    }
    

提交回复
热议问题