try, catch, finally, return的执行顺序

注:文章内容由网络及相关书籍整理而来,如此只为共享知识,给予帮助。

避开云山雾绕的概念拼凑,单刀直入,看实例。
import java.io.*;
public class Mine{
  public static void main(String args[]){
  Mine m=new Mine();
  System.out.println(m.amethod());
  }
public int amethod(){
       try{
           FileInputStream dis =new FileInputStream("Hello.txt"); //步骤1,抛出异常
       }
       catch(Exception e) {
                            System.out.println("No such file found"); //步骤2,catch捕获异常,并执行
                            return -1;                                //步骤4,return 返回
       }
       finally{
               System.out.println("Execute finally"); //步骤3,finally一定会执行,在return之前。
       }
       return 0;
    }
}

运行结果:
C:\java>java   Mine
No such file found
Execute finally
-1

posted on 2008-05-11 00:05  大崩  阅读(401)  评论(0编辑  收藏  举报