public static int NoException(){
int i=10;
try{
System.out.println("i in try block is:"+i);
return --i;
}
catch(Exception e){
--i;
System.out.println("i in catch - form try block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
return --i;
}
}
public static void main(String[] args) {
System.out.println("=============NoException==================");
System.out.println(NoException());
System.out.println("===============================");
}
=============NoException==================
i in try block is:10
i in finally - from try or catch block is:9
8
===============================
public static int NoException1(){运行结果:
int i=10;
try{
System.out.println("i in try block is:"+i);
return --i;
}
catch(Exception e){
--i;
System.out.println("i in catch - form try block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
System.out.println("i in finally block is:"+i);
//return --i;
}
}
=============NoException1==================执行顺序:
i in try block is:10
i in finally - from try or catch block is:9
i in finally block is:8
9
===============================
public static int WithException(){执行结果:
int i=10;
try{
System.out.println("i in try block is:"+i);
i = i/0;
return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
--i;
System.out.println("i in catch block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is--"+i);
--i;
System.out.println("i in finally block is--"+i);
return --i;
}
}
=============WithException==================执行顺序:
i in try block is:10
i in catch - form try block is:10
i in catch block is:9
i in finally - from try or catch block is--8
i in finally block is--7
6
===============================
public static int WithException1(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
return --i;
}catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
return --i;
}finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
System.out.println("i in finally block is:"+i);
//return i;
}
}
=============WithException1==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:9
i in finally block is:8
9
===============================
public static int WithException2(){执行结果:
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
int j = i/0;
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
--i;
System.out.println("i in finally block is:"+i);
return --i;
}
=============WithException2==================执行顺序:
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:10
i in finally block is:8
7
===============================
public static int WithException3(){执行结果:
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
//return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
//int j = i/0;
//return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
--i;
System.out.println("i in finally block is:"+i);
//return --i;
}
return --i;
}
=============WithException3==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:10
i in finally block is:8
7
===============================
注意,除此之外的其他做法都是不可行的,编译器会报错。