A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© user_lqb 中级黑马   /  2016-3-30 11:26  /  1217 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

给定以下JAVA代码,这段代码运行后输出的结果是()
public class Test
{
    public static int aMethod(int i)throws Exception
    {
        try{
            return i / 10;
        }
        catch (Exception ex)
        {
            throw new Exception("exception in a Method");
        } finally{
            System.out.printf("finally");
        }
    }

    public static void main(String [] args)
    {
        try
        {
            aMethod(0);
        }
        catch (Exception ex)
        {
            System.out.printf("exception in main");
        }
        System.out.printf("finished");
    }
}



A finally exception in main finished
  
B  finally finished
  
C  exception in main finally
  
D  finally exception in main finished
  

1 个回复

倒序浏览
B, 主函数 调用aMethod 方法,传入 0, try检测代码块检测异常,无异常时,走finally 一定执行代码块 输出finally,主函数中的try,没有检测出异常,不走catch,输出finished,故 这道题选B
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马