程序输出题20220723

public class Demo {
    public static String sRet = "";
    public static void func(int i)
    {
        try
        {
            if (i%2==0)
            {
                throw new Exception();
            }
        }
        catch (Exception e)
        {
            sRet += "0";
            return;
        
        finally
        {
            sRet += "1";
        }
        sRet += "2";
    }
    public static void main(String[] args)
    {
        func(1);
        func(2);
        System.out.println(sRet);
    }
}
 
“1201”
1.调用func(1),if不符合,直接进入finally,sRet="1"
2.finally语句在没有返回值,故继续向下执行,sRet="12"
3.调用func(2),if符合,sRet="120",此时有return!!!
4.调用finally语句,sRet="1201"
5.已经有return,finally后的语句不执行,sRet="1201"
 
 
 
 
posted @ 2022-07-23 23:07  三思落  阅读(25)  评论(0)    收藏  举报