62,614
社区成员
发帖
与我相关
我的任务
分享
package test;
public class Test {
public int inc() {
int x;
try {
x = 1;
return x;
} catch (Exception e) {
x = 2;
return x;
} finally {
x = 3;
}
}
public StringBuffer inc(StringBuffer x) {
try {
x.append("1");
x = new StringBuffer();
return x;
} catch (Exception e) {
x.append("2");
return x;
} finally {
x.append("3");
x = new StringBuffer("4");
}
}
public static void main(String[] args) {
int x = new Test().inc();
System.out.println(x);
StringBuffer x1 = new StringBuffer(), x2 = new Test().inc(x1);
System.out.println(x1);
System.out.println(x2);
}
}
try{
x=1;
return x;
}catch(){
x=2;
return x;
}finally{
x=3;
// return x;
}
若finally块里面有return,则会覆盖try块里面的return。若没有,当然是按try块的return返回值。
finally块应该是释放资源之类的作用。