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
浙公网安备 33010602011771号