共计 1184 个字符,预计需要花费 3 分钟才能阅读完成。
运行以下代码
public static void main(String[] args) {System.out.println(test());
}
public static String test(){
try {
int t = 1 / 0;
System.out.println("============");
} catch (Exception e) {System.out.println("-------------");
return "exception";
} finally {System.out.println("++++++++++++");
}
return "";
}
输入:
D:\java1.8\jdk1.8.0_60\bin\java.exe ...
Connected to the target VM, address: '127.0.0.1:49697', transport: 'socket'
-------------
++++++++++++
exception
Disconnected from the target VM, address: '127.0.0.1:49697', transport: 'socket'
Process finished with exit code 0
论断:
- 依据输入后果能够看出即便 catch 代码块中蕴含 return 也同样会执行 finally 代码块中的内容
-
finally 代码块执行结束后会继续执行 catch 代码块中的 return 语句
那么 finally 代码块中同样蕴含一个 return 语句又会怎么执行呢?
public static void main(String[] args) {System.out.println(test());
}
public static String test() {
try {
int t = 1 / 0;
System.out.println("============");
} catch (Exception e) {System.out.println("-------------");
return "exception";
} finally {System.out.println("++++++++++++");
return "***************";
}
}
输入:
Connected to the target VM, address: '127.0.0.1:49329', transport: 'socket'
-------------
++++++++++++
***************
Disconnected from the target VM, address: '127.0.0.1:49329', transport: 'socket'
Process finished with exit code 0
论断:
从运行后果中能够看出若 finally 代码块中蕴含了 return 语句则不会再去执行 catch 中的 return 语句
正文完