try catch finally与return的执行顺序

发表于:2007-07-04来源:作者:点击数: 标签:
import java .io.*; public class Mine{ public static void main(String argv[]){ Mine m=new Mine(); System.out.println(m.amethod()); } public int amethod(){ try{ FileInputStream dis =new FileInputStream("Hello.txt"); //1,抛出异常 }catch ( Exc
import java.io.*;
public class Mine{
  public static void main(String argv[]){
  Mine m=new Mine();
    System.out.println(m.amethod());
  }
public int amethod(){
       try{
           FileInputStream dis =new FileInputStream("Hello.txt"); //1,抛出异常
       }catch ( Exception ex) {
               System.out.println("No such file found");   //2.catch捕获异常,并执行
               return -1;                                  //4,return 返回
       }finally{
               System.out.println("Doing finally");  //3.finally一定会执行,在return之前。
       }
        return 0;
    }
}

运行结果:

C:\java>java   Mine
No such file found
Doing finally
-1

原文转自:http://www.ltesting.net