/*
* finally说明
*
* 1.finally当中是一定会被执行的代码,即使catch当中又出现异常、try当中执行了return语句
*/
public class FinallyTest {
public static void main(String[] args) {
numbertest1.chu();
System.out.println("************************************************");
System.out.println(numbertest2.chu());
System.out.println("************************************************");
System.out.println(numbertest3.chu());
}
}
class numbertest1{
static public void chu(){
try {
int a=10;
int b=0;
System.out.println(a/b);
}catch(Exception e) {
System.out.println(e.getStackTrace());
}
System.out.println(15);
}
}
class numbertest2{
static public int chu(){
int x=99;
try {
int a=10;
int b=0;
System.out.println(a/b);
return a;//没有返回a说明该return语句并没有被执行
}catch(Exception e) {
System.out.println(e.getStackTrace());
}
System.out.println(15);
return x;//return x被执行
}
}
class numbertest3{
static public int chu(){
int x=99;
try {
int a=10;
return a;
}catch(Exception e) {
System.out.println(e.getStackTrace());
}
finally {
System.out.println("finally一定会被执行");//即使在try语句当中执行了return语句,finally仍然被执行
}
return x;
}
}
JAVA finally的测试
最新推荐文章于 2024-09-05 11:00:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)