Java_Try_Finally
程序员文章站
2022-04-06 15:54:01
...
public class TestTryCatch {
public static void main(String[] args) {
System.out.println(test1());
}
public static String test1(){
try{
System.out.println("try");
}finally{
System.out.println("finally");
}
return test2();
}
public static String test2(){
System.out.println("2");
return "3";
}
}
The code above is equal with the code below:
public class TestTryCatch {
public static void main(String[] args) {
System.out.println(test1());
}
public static String test1(){
try{
System.out.println("try");
}finally{
System.out.println("finally");
}
String str = test2();
return str;
}
public static String test2(){
System.out.println("2");
return "3";
}
}
The code in finally block will be run after the "return code" run.
Run result below:
try
finally
2
3
推荐阅读