当前位置:网站首页>Execution sequence of try catch finally with return

Execution sequence of try catch finally with return

2022-07-22 12:06:00 InfoQ

argument

  • No matter if there is any abnormality ,finally All the code in the block will execute ;
  • When try and catch There is return when ,finally Still execute ;
  • finally Is in return The following expression is evaluated ( The calculated value is not returned , Instead, save the value you want to return , tube finally What about the code in , The value returned will not change , It's still the value that we saved before ), So the return value of the function is at finally Determined before execution ;
  • finally It is best not to include return, Otherwise the program will exit early , The return value is not try or catch The return value saved in .

argument

situation 1
:try{} catch(){}finally{} return;
             Obviously the program runs in sequence .
situation 2
:try{ return; }catch(){} finally{} return;
           Program execution try In block return Before ( Include return Expression operations in statements ) Code ;
          Re execution finally block , Finally, execute try in return;
         finally Statement after block return, Because the program is try Already in the return So no more execution .
situation 3
:try{ } catch(){return;} finally{} return;
          Program first try, If an exception is encountered catch block ,
          There are abnormal : execute catch in return Before ( Include return Expression operations in statements ) Code , Re execution finally All the code in the statement , Finally, execute catch In block return. finally After that 4 The code at .
          No abnormality : After execution try Again finally Again return.
situation 4
:try{ return; }catch(){} finally{return;}
           Program execution try In block return Before ( Include return Expression operations in statements ) Code ;
           Re execution finally block , because finally In the block return So exit early .
situation 5
:try{} catch(){return;}finally{return;}
           Program execution catch In block return Before ( Include return Expression operations in statements ) Code ;
           Re execution finally block , because finally In the block return So exit early .
situation 6
:try{ return;}catch(){return;} finally{return;}
           Program execution try In block return Before ( Include return Expression operations in statements ) Code ;
           There are abnormal : perform catch In block return Before ( Include return Expression operations in statements ) Code ; Is to perform finally block , because finally In the block return So exit early .
           No abnormality : Is to perform finally block , because finally In the block return So exit early .

summary

Any implementation try  perhaps catch Medium return The statement before , Will execute first finally sentence , If finally exist . If finally There is return sentence , So the program is return 了 , therefore finally Medium return Is bound to be return Of , The compiler the finally Medium return Implemented as a warning.
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/203/202207212110173701.html