java return 和throw
2016-04-22 00:00
225 查看
摘要: java return 和throw
return 在finally之外:
编译:
javac TestThrow.java
执行:
this is FileNotFoundException
this is finally in test
this is exception in main
this is finished in main
结论:
finally的意思是无论try块中有没有发生异常finally块中的代码都会执行。return flag;如果放在finally外,那么try中异常后,catch住然后throw e;方法就中止执行了,则不会执行return flag;
return 在finally之内:
编译:
javac TestThrow.java
执行:
this is FileNotFoundException
this is finally in test
4
this is finished in main
结论:
return flag;放在finally内,try中异常后,catch住然后throw e;方法中止执行抛出异常前会先执行finally块中的语句,而finally中又使方法正常返回。所以就导致了异常没被抛出。
另外,代码修改为如下时,打印值也相同。
注释:
当finally中有return的时候,异常是被存入了栈中,但是最终未被抛给调用者。
总结:
1、不管有没有出现异常,finally块中代码都会执行;
2、当try和catch中有return时,finally仍然会执行;
3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,扔然是之前保存的值),所以函数返回值是在finally执行前确定的;
4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。
参考:
http://bbs.csdn.net/topics/390815821
return 在finally之外:
import java.io.FileInputStream; import java.io.FileNotFoundException; public class TestThrow { public static void main(String[] args) { TestThrow t=new TestThrow(); try{ System.out.println(t.testTry()); }catch(Exception e){ System.out.println("this is exception in main"); }finally { System.out.println("this is finished in main"); } } public int testTry() throws Exception{ FileInputStream fi=null; int flag = 0; try{ fi=new FileInputStream(""); }catch(FileNotFoundException fnfe){ System.out.println("this is FileNotFoundException"); flag = 2; throw new FileNotFoundException(); //return 2; }catch(SecurityException se){ System.out.println("this is SecurityException"); flag = 1; //return 1; }catch(Exception se){ System.out.println("this is Exception"); flag = 3; throw new Exception(); }finally{ System.out.println("this is finally in test"); flag = 4; //return 4; } System.out.println("this is end!!"); return flag; } }
编译:
javac TestThrow.java
执行:
this is FileNotFoundException
this is finally in test
this is exception in main
this is finished in main
结论:
finally的意思是无论try块中有没有发生异常finally块中的代码都会执行。return flag;如果放在finally外,那么try中异常后,catch住然后throw e;方法就中止执行了,则不会执行return flag;
return 在finally之内:
import java.io.FileInputStream; import java.io.FileNotFoundException; public class TestThrow { public static void main(String[] args) { TestThrow t=new TestThrow(); try{ System.out.println(t.testTry()); }catch(Exception e){ System.out.println("this is exception in main"); }finally { System.out.println("this is finished in main"); } } public int testTry() throws Exception{ FileInputStream fi=null; int flag = 0; try{ fi=new FileInputStream(""); }catch(FileNotFoundException fnfe){ System.out.println("this is FileNotFoundException"); flag = 2; throw new FileNotFoundException(); //return 2; }catch(SecurityException se){ System.out.println("this is SecurityException"); flag = 1; return 1; }catch(Exception se){ System.out.println("this is Exception"); flag = 3; //throw new Exception(); return 3; }finally{ System.out.println("this is finally in test"); flag = 4; return 4; } //System.out.println("this is end!!"); //return flag; } }
编译:
javac TestThrow.java
执行:
this is FileNotFoundException
this is finally in test
4
this is finished in main
结论:
return flag;放在finally内,try中异常后,catch住然后throw e;方法中止执行抛出异常前会先执行finally块中的语句,而finally中又使方法正常返回。所以就导致了异常没被抛出。
另外,代码修改为如下时,打印值也相同。
//throw new FileNotFoundException(); return 2;
注释:
当finally中有return的时候,异常是被存入了栈中,但是最终未被抛给调用者。
总结:
1、不管有没有出现异常,finally块中代码都会执行;
2、当try和catch中有return时,finally仍然会执行;
3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,扔然是之前保存的值),所以函数返回值是在finally执行前确定的;
4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。
参考:
http://bbs.csdn.net/topics/390815821
相关文章推荐
- java.lang.OutOfMemoryError: PermGen space
- Spring mvc 上下文初始化过程
- Spring MVC 根上下文 call hierarchy
- Javaweb--如何让登陆页面的表单不默认显示账号和密码
- java Thread sleep yield join wait notify notifyAll
- java视频
- j2ee开发中的“java容器”和“web容器”有什么区别?
- Ubuntu15.10系统下JDK环境的配置
- Android开发常遇问题——Process 'command 'D:....\jdk8_64\bin\java.exe'' finished with non-zero exit value 2
- java 访问权限
- 打包java程序成jar包
- java实现远程控制
- java——认识数组
- spring,hibernate和struts2三大框架整合初印象
- Spring IOC 和 AOP
- java——认识事件与接口
- Java Web基础知识之Listener:监控Servlet的每个动作
- java中volatile关键字的含义
- java堆排序递归代码,无原理版,比较好理解
- eclipse菜单解释及中英对照《二》