`
huiqinbo
  • 浏览: 334036 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

try return finally

阅读更多

Java 中 try , return , finally , throw 使用总结:

问:try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 
    问题补充:
我已经调试过了哈~~是在return 之前执行的哈~~谢谢这位老兄的回答

 

(1)       当 try 中抛出异常且 catch 中有 return 语句, finally 中没有 return 语句, java 先执行 catch 中非 return 语句,再执行 finally 语句,最后执行 catch 中 return 语句。详见情况一。

(2)       当 try 中抛出异常且 catch 中有 return 语句, finally 中也有 return 语句, java 先执行 catch 中非 return 语句,再执行 finally 中非 return 语句,最后执行 finally 中 return 语句,函数返回值为 finally 中返回的值。详见情况二。

(3)       Throw (无能是 catch 中还是非 catch 中)后面不能再跟 code ,否则编译不能通过。详见下面情况三,四,五。

Return , finally 总结:

情况一,代码如下:

public class Test {

public int testTry(){

       FileInputStream fi=null;

      

       try{

           fi=new FileInputStream("");

          

       }catch(FileNotFoundException fnfe){

            System.out.println("this is FileNotFoundException");

            return 1;

       }catch(SecurityException se){

           System.out.println("this is SecurityException");

       }finally{

           System.out.println("this is finally");

       }

       return 0;

}

 

public static void main(String[] args) {

Test t= new Test();

       System. out .println(t.testTry());

}

}

Output :

this is FileNotFoundException

this is finally

1

 

情况二,代码修改如下:

public class Test {

public int testTry(){

       FileInputStream fi=null;

      

       try{

           fi=new FileInputStream("");

          

       }catch(FileNotFoundException fnfe){

            System.out.println("this is FileNotFoundException");

            return 1;

       }catch(SecurityException se){

           System.out.println("this is SecurityException");

       }finally{

           System.out.println("this is finally");

             return 3;

       }

       //return 0;

}

 

public static void main(String[] args) {

Test t= new Test();

       System. out .println(t.testTry());

}

}

Output :

this is FileNotFoundException

this is finally

3

----------------------------------------------------

Return throw 总结:

情况三:

public class Test {

    public static void main(String[] args) {

       Test t=new Test();

       try{

       System.out.println(t.testTry());

       }catch(Exception e){

           System.out.println("this is exception");

       }

      

    }

    public int testTry()throws Exception{

       FileInputStream fi=null;

      

       try{

           fi=new FileInputStream("");

          

       }catch(FileNotFoundException fnfe){

            //System.out.println("this is FileNotFoundException"); 

           throw new Exception();

           return 1;

       }catch(SecurityException se){

           System.out.println("this is SecurityException");

       }finally{

           System.out.println("this is finally");

       }

       return 0;

    }

}

 

>javac Test.java

Test.java:22: 无法访问的语句

                     Return 1;

 

情况四:

public class Test {

    public static void main(String[] args) {

       Test t=new Test();

       try{

       System.out.println(t.testTry());

       }catch(Exception e){

           System.out.println("this is exception");

       }

      

    }

    public int testTry()throws Exception{

       FileInputStream fi=null;

      

       try{

           fi=new FileInputStream("");

          

       }catch(FileNotFoundException fnfe){

            //System.out.println("this is FileNotFoundException"); 

           throw new Exception();

           System.out.println("after throw exception");

          // return 1;

       }catch(SecurityException se){

           System.out.println("this is SecurityException");

       }finally{

           System.out.println("this is finally");

       }

       return 0;

    }

}

>javac Test.java

Test.java:22: 无法访问的语句

                     System.out.println("this is SecurityException");

 

情况五:

public class Test {

    public static void main(String[] args) {

       Test t=new Test();

       try{

       t.testTry();

       }catch(Exception e){

           System.out.println("this is exception");

       }

      

    }

    public void testTry()throws Exception{

       throw new Exception();

       System.out.println("this is testTry method");

    }

}

>javac Test.java

Test.java:22: 无法访问的语句

                    System.out.println("this is testTry method");

 

分享到:
评论
1 楼 mobeicanglang 2012-05-23  
我觉得楼主的例子全部都有错误,题目明明说的是try{}语句中有return语句,又没有说catch块中,也没有说try..catch块中。你的例子都是写的catch块。

相关推荐

    Jungle68#-#[Java] try catch finally,try里有return,finally还执行么?1

    Condition 3: try中有异常,try-catch-finally里都没有return ,finally 之后有个returntry中有异常以后,根据

    try~catch~finally中关于return的问题

    在Java的异常机制中,如果finally中含有return语句,则try和catch中的return语句将会被JVM忽视

    try-finally-return-in-finally.rar_return

    Shouldn t crash on regexps with many nested parentheses.

    try、catch、finally、return 执行顺序.doc

    try、catch、finally、return 执行顺序超详细讲解,包看包会。

    关于Java中的try-catch-finally语句和return

     第二:finally里面不建议放return语句,根据需要,return语句可以放在try和catch里面和函数的后。可行的做法有四:  1、return语句只在函数后出现一次。  2、return语句仅在try和catch里面都出现。  3、...

    try-catch-finally-return-in-finally.rar_return

    The production IfStatement : if ( Expression ) Statement else Statement is evaluated as follows:.

    try-catch-finally执行顺序验证

    try-catch-finally执行顺序验证(左边是.java文件,右边是.class文件) 提示: try、catch块内的return操作编译后会变成把return的值保存到变量var的操作。 总结: try、catch块内的return操作编译后会变成把return的值...

    关于Java中try finally return语句的执行顺序浅析

    主要介绍了关于Java中try finally return语句的执行顺序浅析,需要的朋友可以参考下

    浅谈Java中return和finally的问题

    在Java中当try、finally语句中包含return语句时,执行情况到底是怎样的,finally中的代码是否执行,大家众说纷纭,有的说会执行,有的说不会执行,到底哪种说法正确,下面我们来详细讨论下

    try catch finally的执行顺序深入分析

    首先执行try,如果有异常执行catch,无论如何都会执行finally,当有return以后,函数就会把这个数据存储在某个位置,然后告诉主函数,我不执行了,接下来你执行吧,所以函数就会推出

    Java中finally块执行与return关系深度剖析

    Java finally语句到底是在return之前还是之后执行?Java finally执行深度剖析,具体看这篇博文:http://blog.csdn.net/lanxuezaipiao/article/details/16922895,这是里面相关的源码,欢迎大家下载使用。

    谈谈Java中try-catch-finally中的return语句

    我们知道return语句用在某一个方法中,一是用于返回函数的执行结果,二是用于返回值为void类型的函数中,仅仅是一个return语句(return ;),此时用于结束方法的执行,也即此return后的语句将不会被执行,当然,这种...

    try finally 妙用,防止内存泄漏

    [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]这种写法在IE中100%内存... obj.onclick=function(){ //处理click事件 } obj.onmouseover=function(){ //处理mouseover事件 } try{ return obj; }finally{ o

    java大厂面经、直击BAT

    finally 一定会被执行,如果 finally 里有 return 语句,则覆盖 try/catch 里的 return , 比较爱考的是 finally 里没有 return 语句,这时虽然 finally 里对 return 的值进行了修改,但 return 的值并不改变这种...

    对python中的try、except、finally 执行顺序详解

    print('to return in try') return 'try' except Exception: print('process except') print('to return in except') return 'except' finally: print('to return in finally') return 'finally' test1...

    josonle#Coding-Now#finally语句如何执行1

    - 无论try是否发生异常,finally语句都会执行- 如果try/catch中包含控制转移语句(return、continue、break),finally

    trycatchfinaly

    1. try的意思,就是试着执行里面的语句,所以如果try内部抛出了异常 ... 那么 finally不会被执行,此外的情况,不管是Error还是return,finally块都会执行到。 5. 建议不清楚的拷贝代码或者自己写一份执行了看看。

    python异常处理之try finally不报错的原因

    因为有把python程序打包成exe的需求,所以,有了如下的代码 import time class LoopOver(Exception): def __init__(self, *args, **kwargs): ... return '总共用时:{}秒'.format(self.runtime) if __name__

    java面试题之try中含return语句时代码的执行顺序详解

    主要介绍了关于java中的一道面试题,这套题就是在try中含return语句时代码的执行顺序,这个问题看似简单,却暗藏杀机啊!文中通过一个个例子详细介绍了其中玄机,需要的朋友可以参考学习,下面来一起看看吧。

    JAVA+OOP自测

    2、try {}里有一个return语句,那么紧跟在这个try后的finally {}里的代码会不会被执行,什么时候被执行?B A. 不会执行 B. 会执行,在return前执行 C. 会执行,在return后执行 D. 会执行,可能在return前执行,也...

Global site tag (gtag.js) - Google Analytics