<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=20<span style="color:#000000;">;
</span><span style="color:#0000ff;">try</span><span style="color:#000000;"> {
System.out.println(</span>"try block"<span style="color:#000000;">);
</span><span style="color:#0000ff;">return</span> b += 80<span style="color:#000000;">;
}
</span><span style="color:#0000ff;">catch</span><span style="color:#000000;"> (Exception e) {
System.out.println(</span>"catch block"<span style="color:#000000;">);
}
</span><span style="color:#0000ff;">finally</span><span style="color:#000000;"> {
System.out.println(</span>"finally block"<span style="color:#000000;">);
</span><span style="color:#0000ff;">if</span> (b > 25<span style="color:#000000;">) {
System.out.println(</span>"b>25, b = " +<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>>25, b = 100</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span>
</div>
<p>说明:在return b += 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>"try block"<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>"finally block"<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>"return statement"<span style="color:#000000;">);
</span><span style="color:#0000ff;">return</span> "after return"<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 |
|