java return、finally、throw 执行顺序

package com.bugyun.face;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Question {
    
    public static void main(String[] args) {
       int i = exceptionReturn();
       System.err.println(i);
    }
  
    
    /**
     * 测试 throw 、 return 、 finally 的执行顺序
     * 执行顺序:
     *      throw -> catche -> return -> finally
     *      方法返回的值是2
     * @return    
     * @author beyond
     * @data:2017年5月31日上午9:57:39
     */
    public static int exceptionReturn(){
        int i = 1;
        try {
            System.out.println("  throw 部分");
            throw new Exception();             
        } catch (Exception e) {
            System.out.println("  catch 部分 ");
            return ++i;
        }finally {
            System.out.println("  finally 部分 begin , i = "+i);
            ++i;
            System.out.println("  finally 部分 end , i = "+i);
        }
    }
    
}

  调用方法返回值2,的顺序不稳定,但其他执行顺序稳定



 

猜你喜欢

转载自bugyun.iteye.com/blog/2377040
今日推荐