finally
是无论如何都要执行的,除非在try/catch
的语句块中使用的System.exit()
为了弄清楚Java中try/catch/finally
中使用return
语句的跳转,使用下面示例:
1. 首先在try
中使用return
,finally
中不使用return
public class App {
public static void main(String[] args) throws Exception {
App app = new App();
String res = app.test();
System.out.println(res);
}
public String test() {
try {
System.out.println("正在执行 try");
return new String("返回 try");
} catch (Throwable throwable) {
System.out.println("异常");
return new String("catch");
} finally {
System.out.println("正在执行 finally");
// return new String("返回 finally");
}
}
}
执行结果
由此可见执行顺序:try中语句——>finally中语句——>try中return语句
2. 在try
中使用return
,finally
中也使用return
public class App {
public static void main(String[] args) throws Exception {
App app = new App();
String res = app.test();
System.out.println(res);
}
public String test() {
try {
System.out.println("正在执行 try");
return new String("返回 try");
} catch (Throwable throwable) {
System.out.println("异常");
return new String("catch");
} finally {
System.out.println("正在执行 finally");
return new String("返回 finally");
}
}
}
执行结果
执行顺序:try中语句——>finally中语句——>finally中的return
3. 在catch
中使用return
public class App {
public static void main(String[] args) throws Exception {
App app = new App();
String res = app.test();
System.out.println(res);
}
public String test() {
try {
System.out.println("正在执行 try");
throw new Throwable("Some error");
} catch (Throwable throwable) {
System.out.println("异常");
return new String("catch");
} finally {
System.out.println("正在执行 finally");
return new String("返回 finally");
}
}
}
执行顺序和前面类似:try中语句——>catch中语句——>finally中语句——>finally中return
综上所述
在执行完try
和catch
语句中后,都会执行finally
中的语句,此时,如果finally
中有return
就直接从这里跳出。如果没有的话,就从try
或者catch
中的return
中返回。