黑马程序员技术交流社区

标题: 一道关于try....catch的题 [打印本页]

作者: user_lqb    时间: 2016-3-30 11:26
标题: 一道关于try....catch的题
给定以下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
  
作者: 小灰辉    时间: 2016-3-30 12:13
B, 主函数 调用aMethod 方法,传入 0, try检测代码块检测异常,无异常时,走finally 一定执行代码块 输出finally,主函数中的try,没有检测出异常,不走catch,输出finished,故 这道题选B




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2