public class TryCatchFinally {
public static void main(String[] args) {
//System.out.println(fun1());
System.out.println(fun2());
}
//存在异常的情况
//产生异常,try{}里面出现异常的位置往后都不执行了-->catch-->finally(t=2)-->最后返回t=2
public static int fun1(){
int t=0;
try{
int i =3/0;
t=1;
System.out.println("out1");
return t;
}catch(Exception e){
e.printStackTrace();
}finally{
//return 2;
t=2;
System.out.println("out2");
}
System.out.println("out3");
//return 3;
return t;
}
//没有异常的情况
//return t=1-->finally-->程序结束。
public static int fun2(){
int t=0;
try{
t=1;
System.out.println("out1");
return t;
}catch(Exception e){
e.printStackTrace();
}finally{
//return 2;
t=2;
System.out.println("out2");
}
System.out.println("out3");
//return 3;
return t;
}
//finally里面不要 写return,这样必然会返回,没有意义
}
public static void main(String[] args) {
//System.out.println(fun1());
System.out.println(fun2());
}
//存在异常的情况
//产生异常,try{}里面出现异常的位置往后都不执行了-->catch-->finally(t=2)-->最后返回t=2
public static int fun1(){
int t=0;
try{
int i =3/0;
t=1;
System.out.println("out1");
return t;
}catch(Exception e){
e.printStackTrace();
}finally{
//return 2;
t=2;
System.out.println("out2");
}
System.out.println("out3");
//return 3;
return t;
}
//没有异常的情况
//return t=1-->finally-->程序结束。
public static int fun2(){
int t=0;
try{
t=1;
System.out.println("out1");
return t;
}catch(Exception e){
e.printStackTrace();
}finally{
//return 2;
t=2;
System.out.println("out2");
}
System.out.println("out3");
//return 3;
return t;
}
//finally里面不要 写return,这样必然会返回,没有意义
}