Java finally return 类型 迷惑
程序员文章站
2022-06-06 09:35:45
...
package com.bobo;
public class Test {
/**
* QQ:15221860
* @param args
*/
public static void main(String[] args) {
System.out.println(getA());
System.out.println(getStudent().name);
}
static String getA() {
String a = "1";
try {
return a;
} catch (Exception e) {
a = "2";
return a;
} finally {
System.out.println("hi");
a = "3";
}
}
static Student getStudent() {
Student a = new Student();
a.name = "1";
try {
return a;
} catch (Exception e) {
a.name = "2";
return a;
} finally {
System.out.println("hi");
a.name = "3";
}
}
static class Student {
String name = "";
}
}
public class Test {
/**
* QQ:15221860
* @param args
*/
public static void main(String[] args) {
System.out.println(getA());
System.out.println(getStudent().name);
}
static String getA() {
String a = "1";
try {
return a;
} catch (Exception e) {
a = "2";
return a;
} finally {
System.out.println("hi");
a = "3";
}
}
static Student getStudent() {
Student a = new Student();
a.name = "1";
try {
return a;
} catch (Exception e) {
a.name = "2";
return a;
} finally {
System.out.println("hi");
a.name = "3";
}
}
static class Student {
String name = "";
}
}
推荐阅读
-
浅谈Java finally语句到底是在return之前还是之后执行(必看篇)
-
浅谈Java finally语句到底是在return之前还是之后执行(必看篇)
-
Java中finally和return的关系实例解析
-
Java中finally和return的关系实例解析
-
Java异常处理中finally和return的结合使用
-
Java基础之finally语句与return语句详解
-
Java finally return 类型 迷惑
-
Java finally return 类型 迷惑
-
Java中try与finally以及return语句的执行顺序
-
Head First Java: Java 里面的 try、catch、finally 与 return