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

来源:互联网 发布:windows 欢迎界面图片 编辑:程序博客网 时间:2024/06/08 10:59

Javatryreturn finallythrow使用总结:

 

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

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

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

Returnfinally总结:

情况一,代码如下:

public class Test {

public int testTry(){

       FileInputStreamfi=null;

      

       try{

           fi=newFileInputStream("");

          

       }catch(FileNotFoundExceptionfnfe){

            System.out.println("this isFileNotFoundException");

            return 1;

       }catch(SecurityExceptionse){

           System.out.println("thisis SecurityException");

       }finally{

           System.out.println("thisis finally");

       }

       return0;

}

 

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(){

       FileInputStreamfi=null;

      

       try{

           fi=newFileInputStream("");

          

       }catch(FileNotFoundExceptionfnfe){

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

            return 1;

       }catch(SecurityExceptionse){

           System.out.println("thisis SecurityException");

       }finally{

           System.out.println("thisis finally");

            return 3;

       }

       //return0;

}

 

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 {

    publicstatic void main(String[] args) {

       Testt=new Test();

       try{

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

       }catch(Exceptione){

           System.out.println("thisis exception");

       }

      

    }

    publicint testTry()throws Exception{

       FileInputStreamfi=null;

      

       try{

           fi=newFileInputStream("");

          

       }catch(FileNotFoundExceptionfnfe){

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

           thrownew Exception();

           return 1;

       }catch(SecurityExceptionse){

           System.out.println("thisis SecurityException");

       }finally{

           System.out.println("thisis finally");

       }

       return0;

    }

}

 

>javac Test.java

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

                     Return 1;

 

情况四:

public class Test {

    publicstatic void main(String[] args) {

       Testt=new Test();

       try{

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

       }catch(Exceptione){

           System.out.println("thisis exception");

       }

      

    }

    publicint testTry()throws Exception{

       FileInputStreamfi=null;

      

       try{

           fi=newFileInputStream("");

          

       }catch(FileNotFoundExceptionfnfe){

            //System.out.println("this isFileNotFoundException"); 

           thrownew Exception();

           System.out.println("afterthrow exception");

          // return 1;

       }catch(SecurityExceptionse){

           System.out.println("thisis SecurityException");

       }finally{

           System.out.println("thisis finally");

       }

       return0;

    }

}

>javac Test.java

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

                    System.out.println("this isSecurityException");

 

情况五:

public class Test {

    publicstatic void main(String[] args) {

       Testt=new Test();

       try{

       t.testTry();

       }catch(Exceptione){

           System.out.println("thisis exception");

       }

      

    }

    publicvoid testTry()throws Exception{

       thrownew Exception();

       System.out.println("thisis testTry method");

    }

}

>javac Test.java

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

                    System.out.println("thisis testTry method");

 

 

原创粉丝点击