任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally存在的话。
如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,
编译器把finally中的return视为为一个warning。
一、在java中会经常遇到关于捕获异常的处理, 下面大致总结了所有关于try catch finally return执行顺序的时候的问题,其实最重要的是处理 finally 关键词语句的问题:
1、在finally 后面 不跟 return 语句。
1.1 在此种情况下,try{ return ;} catch{} finally{},如果try中没有异常就执行finally中语句,如果有异常就执行catch语句和finally中的语句。
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
----return
2
无异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/1;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果:
----try
----finally
2
1.2 在这种形式下try{ }catch{}finally{},如果try中没有异常就执行finally中的语句,如果有异常就执行catch和finally中的语句
有异常
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果为:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
----return
2
无异常
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/1;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果为:
----try
----finally
----return
3
1.3 在这种形式下try{ }catch{ return;}finally{},如果try中没有异常就执行finally中的语句,如果有异常就执行catch和finally中的语句
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
return a;
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果为:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
1
无异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/1;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
return a;
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果为:
----try
----finally
----return
3
1.4.在这种形式下try{ return; }catch{ return;}finally{},如果try中没有异常就执行finally中的语句,如果有异常就执行catch和finally中的语句
无异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/1;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
return a;
}finally {
System.out.println("----finally");
++a;
}
// System.out.println("----return");
// return a;
}
结果:
----try
----finally
2
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
return a;
}finally {
System.out.println("----finally");
++a;
}
// System.out.println("----return");
// return a;
}
结果为:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
1
2.在finally 后面跟 return 语句
2.1在这种形式下try{ }catch{ }finally{} return;,如果try中没有异常就执行finally中和finally后面的语句,如果有异常就执行catch、finally中和finally后面的语句
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
----return
2
无异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/1;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果为:
----try
----finally
----return
3
2.2在这种形式下try{ return; }catch{ }finally{} return;,如果try中没有异常就执行finally中的语句,如果有异常就执行catch、finally中和finally后面的语句
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果为:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
----return
2
无异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/1;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
}finally {
System.out.println("----finally");
++a;
}
System.out.println("----return");
return a;
}
结果为:
----try
----finally
2
2.3在这种形式下try{ }catch{ return; }finally{ return;},如果try中没有异常就执行finally和finally后面中的语句,如果有异常就执行catch和finally中的语句
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
return a;
}finally {
System.out.println("----finally");
++a;
return a;
}
// System.out.println("----return");
// return a;
}
结果为:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
2
2.4在这种形式下try{return; }catch{ return; }finally{ return;},如果try中没有异常就执行finally和finally后面中的语句,如果有异常就执行catch和finally中的语句
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
return a;
}finally {
System.out.println("----finally");
++a;
return a;
}
// System.out.println("----return");
// return a;
}
结果为:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
2
有异常:
public static int method_01(){
int a =1 ;
try{
System.out.println("----try");
a = 2/0;
return a;
}catch (Exception e){
e.printStackTrace();
System.out.println("----catch");
return a;
}finally {
System.out.println("----finally");
++a;
// return a;
}
// System.out.println("----return");
// return a;
}
结果为:
----try
java.lang.ArithmeticException: / by zero
at com.wsl.demo.demo.method_01(demo.java:13)
at com.wsl.demo.demo.main(demo.java:6)
----catch
----finally
1
二、关于try,catch, finally,return 一些面试题:
题一:
public static int method_01(){
int a =1 ;
try{
return a;
}catch (Exception e){
System.out.println("111111");
}finally {
++a;
return a;
}
}
结果为:2
题二:
public static int method_02(){
int a=1;
try{
int u=1/0;
return a;
}catch (Exception e){
System.out.println("111111");
}finally {
++a;
return a;
}
}
结果:
111111
2
题三:
public static int method_02() {
int a = 1;
try {
int u=1/0;
return a;
} catch ( Exception e) {
System.out.println("111111");
return a;
} finally {
++a;
}
}
结果:
111111
1
题四:
public static int method_04() {
int a = 1;
try {
return a;
} catch (Exception e) {
System.out.println("111111");
} finally {
++a;
}
return a;
}
结果为:
2
题五:
public static int method_05() {
int a = 1;
try {
int u=1/0;
return a;
} catch (Exception e) {
System.out.println("11");
return a+1;
} finally {
++a;
System.out.println("22");
}
}
结果为:
11
22
2
题六:
public static int method_06() {
int a = 1;
try {
int u=1/0;
return a;
} catch ( ArithmeticException e) {
System.out.println("11");
return a+1;
} catch (Exception e) {
System.out.println("22");
return a;
}finally {
++a;
}
}
结果为:
11
2
题七
public static int method_07() {
int a = 1;
try {
int u=1/0;
return a;
} catch ( NullPointerException e) {
System.out.println("11");
return a+1;
} catch (Exception e) {
System.out.println("22");
return a;
}finally {
++a;
}
}
结果为:
22
1
三、归纳:
try catch
try 里面的return可以返回但不能结束方法.
catch ,finnally ,或者正常的return才可以结束本方法.
因为finnally始终要执行,所以当catch和finnally里都有return ,catch里的这个return不结束方法
四、特殊情况:可以让finally不执行
在异常处理中,finally一般都会执行,但有时可以使其不执行,我们可以看个程序。
public class demo {
public static void main(String[] args) {
try{
System.out.println("-cus-try");
throw new NullPointerException();
}catch (Exception e){
e.printStackTrace();
System.out.println("-cus-execption");
System.exit(10);
}finally {
System.out.println("-cus-finally");
}
}
}
输出结果:
"C:\Program Files\Java\jdk1.8.0_201\bin\java.exe"
-cus-try
java.lang.NullPointerException
at com.abc.demo.demo.main(demo.java:7)
-cus-execption
Process finished with exit code 10
可以看出并没有执行finally中的语句,这主要是由于在catch中有System.exit(10),它可以使虚拟机直接停止运行,从而使 finally不执行。
五,总结:
捕获异常和抛出异常的区别:
try里的是可能抛出异常的语句;
catch是用来捕获异常的;
一个方法如果完全有能力自己处理发生的错误就可以自己处理,如果处理不了这个错误,就可以用throw重新抛出
为什么会抛出异常:
1、为了防止遇到异常的时候程序崩溃,影响用户。
2、抛出的异常也方便了开发人员调试,让错误有迹可循。
3、还能定位错误出在什么地方,当出现了问题时,起码知道哪里出问题了。
4、异常处理让你能控制你的应用按照你的逻辑走(例如:给用户提示出错了等)。
处理异常的技巧:
1、避免过大的try块,不要把不会出现异常的代码放到try块里面,尽量保持一个try块对应一个或多个异常。
2、细化异常的类型,不要不管什么类型的异常都写成Excetpion。
3、catch块尽量保持一个块捕获一类异常,不要忽略捕获的异常,捕获到后要么处理,要么转译,要么重新抛出新类型的异常。
4、不要把自己能处理的异常抛给别人。
5、不要用try…catch参与控制程序流程,异常控制的根本目的是处理程序的非正常情况。
异常语句的执行顺序:
任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally存在的话。
如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,
编译器把finally中的return视为为一个warning。
参考