深入理解finally关键字,Finally到底是在return前面执行还是在return后面执行

论坛 期权论坛     
选择匿名的用户   2021-5-30 01:44   1198   0
<div class="blogpost-body" id="cnblogs_post_body">
<h1>一:2种finally不会执行的情况</h1>
<p><span style="color:#ff0000;">  a.在try语句之前就return了</span></p>
<p><span style="color:#ff0000;">  b.try语句中有System.exit();语句</span></p>
<h1>二:finally语句在<span style="color:#ff0000;">return执行之后,return返回之前</span>执行</h1>
<p>  例1:</p>
<div class="cnblogs_code">
  <pre class="blockcode"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> FinallyTest1 {
    </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> main(String[] args) {
        test1();
    }
    </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">int</span><span style="color:#000000;"> test1(){
        </span><span style="color:#0000ff;">int</span> b&#61;20<span style="color:#000000;">;
        </span><span style="color:#0000ff;">try</span><span style="color:#000000;"> {
            System.out.println(</span>&#34;try block&#34;<span style="color:#000000;">);
            </span><span style="color:#0000ff;">return</span> b &#43;&#61; 80<span style="color:#000000;">;
        }
        </span><span style="color:#0000ff;">catch</span><span style="color:#000000;"> (Exception e) {
            System.out.println(</span>&#34;catch block&#34;<span style="color:#000000;">);
        }
        </span><span style="color:#0000ff;">finally</span><span style="color:#000000;"> {
            System.out.println(</span>&#34;finally block&#34;<span style="color:#000000;">);
            </span><span style="color:#0000ff;">if</span> (b &gt; 25<span style="color:#000000;">) {
                System.out.println(</span>&#34;b&gt;25, b &#61; &#34; &#43;<span style="color:#000000;"> b);
            }
        }
        </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> b;
    }
}</span></pre>
</div>
<p>console:</p>
<div class="cnblogs_code">
  <img alt="" class="code_img_closed" id="code_img_closed_a71ea1ac-36a9-4898-a488-f367cb0373fd" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8f900a89c6347c561fdf2122f13be562.gif">
  <img alt="" class="code_img_opened" id="code_img_opened_a71ea1ac-36a9-4898-a488-f367cb0373fd" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-961ddebeb323a10fe0623af514929fc1.gif">
  <div class="cnblogs_code_hide" id="cnblogs_code_open_a71ea1ac-36a9-4898-a488-f367cb0373fd">
   <pre class="blockcode"><span style="color:#0000ff;">try</span><span style="color:#000000;"> block
</span><span style="color:#0000ff;">finally</span><span style="color:#000000;"> block
b</span>&gt;25, b &#61; 100</pre>
  </div>
  <span class="cnblogs_code_collapse">View Code</span>
</div>
<p>说明:在return b &#43;&#61; 80;后先执行finally语句</p>
<p>再来一个例子加强这个事实</p>
<p>例2</p>
<div class="cnblogs_code">
  <pre class="blockcode"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> FinallyTest1 {
    </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> main(String[] args) {
        System.out.println(test11());
    }
    </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span><span style="color:#000000;"> String test11() {
        </span><span style="color:#0000ff;">try</span><span style="color:#000000;"> {
            System.out.println(</span>&#34;try block&#34;<span style="color:#000000;">);
            </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> test12();
        } </span><span style="color:#0000ff;">finally</span><span style="color:#000000;"> {
            System.out.println(</span>&#34;finally block&#34;<span style="color:#000000;">);
        }
    }
    </span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span><span style="color:#000000;"> String test12() {
        System.out.println(</span>&#34;return statement&#34;<span style="color:#000000;">);
        </span><span style="color:#0000ff;">return</span> &#34;after return&#34;<span style="color:#000000;">;
    }
}</span></pre>
</div>
<p>console:</p>
<div class="cnblogs_code">
  <img alt="" class="code_img_closed" id="code_img_closed_5465cfa4-a07d-4efe-8320-574ed447e3f8" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8f900a89c6347c561fdf2122f13be562.gif">
  <img alt="" class="code_img_opened" id="code_img_opened_5465cfa4-a07d-4efe-8320-574ed447e3f8" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-961ddebeb323a10fe0623af514929fc1.gif">
  <div class="cnblogs_code_hide" id="cnblogs_code_open_5465cfa4-a07d-4efe-8320-574ed447e3f8">
   <pre class="blockcode"><span style="color:#0000ff;">try</span><span style="color:#000000;"> block
</span><span style="color:#0000ff;">return</span><span style="color:#000000;"> statement
</span><span style="color:#0000ff;">finally</span><span style="color:#000000;"> block
after </span><span style="color:#0000ff;">return</span></pre>
  </div>
  <span class="cnblogs_code_collapse">View Co
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP