try,catch,finally

public class Foo {
public static int calc() throws IOException{
int ret = 0;
try {
++ret;
throw new IOException("try");
} catch (IOException ioe) {
--ret;
return ret;     //finally无论如何都会执行,如果finally后没有return 则会回到try或者catch中输出它们中的变量值
                   // 有return则直接输出自己处理后的值
                  //finally在try或catch 的return之前执行。
}finally{
++ret;

}
}

输出0

 

 

package exercise;

import java.io.IOException;
public class Foo {
public static int calc() throws IOException{
int ret = 0;
try {
++ret;
throw new IOException("try");
} catch (IOException ioe) {
--ret;
return ret;         //finally无论如何都会执行,如果finally后没有return 则会回到try或者catch中输出它们中的变量值
                      // 有return则直接输出自己处理后的值
                      //finally在try或catch 的return之前执行。
}finally{
++ret;
return ret;

}
}
public static void main(String[] args) throws IOException{
System.out.println( calc());
}
}

 

输出1

posted on 2016-10-26 14:11  Dos尚  阅读(187)  评论(0)    收藏  举报