<meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE">
<meta content="OpenOffice.org 3.2 (Linux)" name="GENERATOR"> <style type="text/css"> <!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } --> </style> public int testFinally(){int x=10;
try{
x=20;
return x;
}catch(Exception e){
x=100;
}finally{
x=200;
System.out.println("int finally="+x);
}
return x;
}
在该方法中返回的x值是20,因为finally语句必须执行,所以执行x=20;这条语句后,遇到 <meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE">
<meta content="OpenOffice.org 3.2 (Linux)" name="GENERATOR"> <style type="text/css"> <!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } --> </style> return 语句, 将会暂停return 语句,但是return 语句内的值是20 , 将return中的值保存在内存中, 转而执行finally 语句, finally 语句执行完毕后,会执行刚return 语句,即返回20,但若是finally语句快中也有return语句的时候则只会执行finally语句中的renturn,而不会执行try或catch语句块中的return语句。