packagett;public classFinallyWorks {/***@paramargs*/
public static voidmain(String[] args) {//finallyWork();
/**
* catch block / by zero
* finally block invoked!!!
* finally block invoked, ret is 1
* throwReturn:0
*
*从输出结果中可以看出程序在int temp = 10/0;这一行抛出异常,直接进入catch块,首先输出打印catch block...,继续往下执行时碰到return语句,由于程序
*存在finally语句,在程序返回之前需要执行finally语句。那么此时程序会将return的结果值暂时存起来,继续执行finally,从输出上可以看出finally执行后ret
*的值变为了1,而整个方法最终的返回结果是0,说明return的是之前暂存的值。
**/
//System.out.println("throwReturn:" + throwReturn());
/**
* catch block / by zero
* finally block invoked!!!
* finally block invoked, ret is 1
* finallyReturn:1
*
*从输出结果中可以看出程序在int temp = 10/0;这一行抛出异常,直接进入catch块,首先输出打印catch block...,继续往下执行时碰到return语句,由于程序
*存在finally语句,在程序返回之前需要执行finally语句。那么此时程序会将return的结果值暂时存起来,继续执行finally,从输出上可以看出finally执行后ret
*的值变为了1,有在finally块中碰到了return语句,方法就直接返回了,所以方法结果返回了1。
**/
//System.out.println("finallyReturn:" + finallyReturn());
/**
* catch block / by zero
* finally block invoked!!!
* finally block invoked, ret is 1
* 捕获到throwException方法抛出的异常,/ by zero
*
*从输出结果中可以看出在调用throwException方法是出现异常,程序进入该方法的catch块中,输出:catch block / by zero
*由于存在finally,程序会先执行完finally语句输出:finally block invoked!!! 和 finally block invoked, ret is 1
*然后将捕获到的异常抛向上层。上层的main方法catch到这个异常之后会输出:捕获到throwException方法抛出的异常,/ by zero
*《注意throwException:那句打印是不会输出的》
**/
/*try {
System.out.println("throwException:" + throwException());
} catch (Exception e) {
System.out.println("捕获到throwException方法抛出的异常," + e.getMessage());
}*/
/**
* catch block / by zero
* finally block invoked!!!
* finally block invoked, ret is 1
* finallyThrowException:1
*
*从输出结果中可以看出在调用finallyThrowException方法是出现异常,程序进入该方法的catch块中,输出:catch block / by zero
*由于存在finally,程序会先执行完finally语句输出:finally block invoked!!! 和 finally block invoked, ret is 1
*之后程序执行到finally块中return语句,直接返回了ret的值,主方法接受到这个返回值后输出:finallyThrowException:1
*《注意主方法中catch块代码并没有被执行,这就说明了finallyThrowException方法中异常被丢失了》
**/
try{
System.out.println("finallyThrowException:" +finallyThrowException());
}catch(Exception e) {
System.out.println("捕获到finallyThrowException方法抛出的异常," +e.getMessage());
}
}public static int throwException() throwsException{int ret = 0;try{
ret= 10/0;
System.out.println("ret:" +ret);returnret;
}catch(Exception e){
System.out.println("catch block " +e.getMessage());throwe;
}finally{
System.out.println("finally block invoked!!!");
ret++;
System.out.println("finally block invoked, ret is " +ret);
}
}public static int finallyThrowException() throwsException{int ret = 0;try{
ret= 10/0;
System.out.println("ret:" +ret);returnret;
}catch(Exception e){
System.out.println("catch block " +e.getMessage());throwe;
}finally{
System.out.println("finally block invoked!!!");
ret++;
System.out.println("finally block invoked, ret is " +ret);returnret;
}
}public static intthrowReturn(){int ret = 0;try{
ret= 10/0;
ret++;returnret;
}catch(Exception e){
System.out.println("catch block " +e.getMessage());//ret++;
returnret;
}finally{
System.out.println("finally block invoked!!!");
ret++;
System.out.println("finally block invoked, ret is " +ret);
}
}public static intfinallyReturn(){int ret = 0;try{
ret= 10/0;
ret++;returnret;
}catch(Exception e){
System.out.println("catch block " +e.getMessage());//ret++;
returnret;
}finally{
System.out.println("finally block invoked!!!");
ret++;
System.out.println("finally block invoked, ret is " +ret);returnret;
}
}/*** 当java中的异常不允许我们回到异常抛出的地点时,我们可以将try块放到循环里,
* 这样程序就又可以回到异常的抛出点了,可以同时设置一个计数器,
* 当累积尝试一定的次数后程序就退出。*/
public static voidfinallyWork(){int count = 0;while(true){try{if(count++ == 0){throw new Exception("my error");
}
System.out.println("invoked ...");
}catch(Exception e){
System.out.println("catched exception:" +e.getMessage());
}finally{
System.out.println("finally block invoked!!!");if(count == 2){break;
}
}
}
}
}