Java基礎之finally語句與return語句詳解
一、return語句執(zhí)行順序
finally語句是在return語句執(zhí)行之后,return語句返回之前執(zhí)行的
package exception;
public class Demo06 {
public static void main(String[] args) {
System.out.println(func());
}
public static int func(){
int a = 10;
try{
System.out.println("try中的代碼塊");
return a += 10;
}catch (Exception e){
System.out.println("catch中的代碼塊");
}finally {
System.out.println("finally中的代碼塊");
if(a > 10){
System.out.println("a > 10,"+"a="+a);
}
}
return a += 50;
}
}
運行結果:
try中的代碼塊
finally中的代碼塊
a > 10,a=20
20
注意:
a > 10,a=20的結果說明了return a += 10已經(jīng)執(zhí)行了,但是沒有直接返回,而是先去執(zhí)行finally語句的內(nèi)容,然后再去返回結果
二、覆蓋問題
finally塊中的return語句會覆蓋try塊的return返回
package exception;
public class Demo07 {
public static void main(String[] args) {
System.out.println(func());
}
public static int func(){
int a = 10;
try{
System.out.println("try中的代碼塊");
return a += 10;
}catch (Exception e){
System.out.println("catch中的代碼塊");
}finally {
System.out.println("finally中的代碼塊");
if(a > 10){
System.out.println("a>10,"+"a="+a);
}
return 100;
}
}
}
運行結果:
try中的代碼塊
finally中的代碼塊
a>10,a=20
100
注意:
(1)如果try中有return語句,finally中也有return語句,最終執(zhí)行的是finally中的return語句
(2)如果finally代碼塊中寫了return語句,那么finally之后的return語句就變成不可到達的語句,需要注釋掉,否則編譯不過
如果finally語句沒有return語句覆蓋返回值,那么原來的返回值可能因為finally里的修改而改變也有可能不變
(1)測試1
package exception;
public class Demo08 {
public static void main(String[] args) {
System.out.println(func());
}
public static int func(){
int a = 10;
try{
System.out.println("try中的代碼塊");
return a += 20;
}catch (Exception e){
e.printStackTrace();
System.out.println("catch中的代碼塊");
}finally {
System.out.println("finally中的代碼塊");
a += 20;
if(a > 10){
System.out.println("a > 10,a="+a);
}
a += 20;
}
return 200;
}
}
運行結果:
try中的代碼塊
finally中的代碼塊
a > 10,a=50
30
注意:
對于基本數(shù)據(jù)類型來說,finally中對返回值的修改不會影響try中的返回變量的值
(2)測試2
package exception;
import java.util.HashMap;
import java.util.Map;
public class Demo09 {
public static void main(String[] args) {
System.out.println(getMap().get("KEY").toString());
}
public static Map<String,String> getMap(){
Map<String,String> map = new HashMap<>();
map.put("KEY","INIT");
try{
map.put("KEY","try");
return map;
}catch (Exception e){
e.printStackTrace();
map.put("KEY","catch");
}finally {
map.put("KEY","finally");
map = null;
}
return map;
}
}
運行結果:
finally
注意:
對于引用數(shù)據(jù)類型來說,finally中對返回值的修改會影響try中的返回變量的值
三、異常情況
try塊中的return語句在異常的情況下不會被執(zhí)行
package exception;
public class Demo10 {
public static void main(String[] args) {
System.out.println(func());
}
public static int func(){
int a = 10;
try{
System.out.println("try中的代碼塊");
a = a/0;
return a += 50;
}catch (Exception e){
a += 15;
System.out.println("catch中的代碼塊");
}finally {
System.out.println("finally中的代碼塊");
if(a > 20){
System.out.println("a > 20,a ="+a);
}
a += 10;
}
return a;
}
}
運行結果:
try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 20,a =25
35
注意:
try語句塊中發(fā)生異常,try語句異常后的內(nèi)容不會執(zhí)行,return語句也不會執(zhí)行,執(zhí)行的是捕獲到的catch語句塊和finally語句塊
try中發(fā)生異常時,return寫在catch語句中
package exception;
public class Demo11 {
public static void main(String[] args) {
System.out.println(func());
}
public static int func(){
int a = 10;
try{
System.out.println("try中的代碼塊");
a = a /0;
return a += 10;
}catch (Exception e){
System.out.println("catch中的代碼塊");
return a += 15;
}finally {
System.out.println("finally中的代碼塊");
if (a > 10){
System.out.println("a > 10, a = "+a);
}
a += 50;
System.out.println(a);
}
}
}
運行結果:
try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 10, a = 25
75
25
注意:
try中發(fā)生異常之后,catch中的return語句先執(zhí)行,確定了返回值之后(保存起來,finally中的語句對返回值無影響)再去finally語句塊,執(zhí)行完之后再返回a的值,finally中對a的修改對返回值無效
四、finally語句一定會被執(zhí)行嗎?
(1)當程序進入try語句之前就出現(xiàn)異常時,會直接結束
(2)try語句塊中有強制退出時也不會執(zhí)行finally語句塊中的代碼
System.exit(0);
代碼示例:
package exception;
public class Demo12 {
public static void main(String[] args) {
int a = 10;
try{
System.out.println("try block");
System.exit(0);
}catch (Exception e){
System.out.println("catch block");
}finally {
System.out.println("finally block");
}
}
}
運行結果:
try block
到此這篇關于Java基礎之finally語句與return語句詳解的文章就介紹到這了,更多相關java finally語句與return語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot用多線程批量導入數(shù)據(jù)庫實現(xiàn)方法
這篇文章主要介紹了SpringBoot用多線程批量導入數(shù)據(jù)庫實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02
SpringBoot+Spring?Data?JPA整合H2數(shù)據(jù)庫的示例代碼
H2數(shù)據(jù)庫是一個開源的關系型數(shù)據(jù)庫,本文重點給大家介紹SpringBoot+Spring?Data?JPA整合H2數(shù)據(jù)庫的示例代碼,感興趣的朋友跟隨小編一起看看吧2022-02-02
Java中HashMap和Hashtable的區(qū)別淺析
這篇文章主要介紹了Java中HashMap和Hashtable的區(qū)別淺析,本文總結了6條它們之間的不同之處,需要的朋友可以參考下2015-03-03

