黑马程序员技术交流社区

标题: 一道关于throw和try/catch的面试题。 [打印本页]

作者: halohoop    时间: 2015-7-28 17:47
标题: 一道关于throw和try/catch的面试题。
类Demo1:

  1. class  Demo1
  2. {
  3.     public static void func()
  4.     {
  5.         try
  6.         {
  7.             throw new Exception();
  8.             System.out.println("A");//(**)
  9.         }
  10.         catch (Exception e)
  11.         {
  12.             System.out.println("B");
  13.         }
  14.     }
  15.     public static void main(String[] args)
  16.     {
  17.         try
  18.         {
  19.             func();
  20.         }
  21.         catch (Exception e)
  22.         {
  23.             System.out.println("C");
  24.         }
  25.         System.out.println("D");
  26.     }
  27. }
复制代码

类Demo2:

  1. class Demo2
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         try
  6.         {
  7.             showExec();
  8.             System.out.println("A");
  9.         }
  10.         catch (Exception e)
  11.         {
  12.             System.out.println("B");
  13.         }
  14.         finally
  15.         {
  16.             System.out.println("C");
  17.         }
  18.         System.out.println("D");
  19.     }
  20.     public static void showExec() throws Exception
  21.     {
  22.         throw new Exception();
  23.     }
  24. }
复制代码

问这两个类是否能编译通过,为什么?
解析:


作者: fmi110    时间: 2015-7-28 18:36
本帖最后由 fmi110 于 2015-7-28 18:38 编辑

复习了 原来代码块中包含无法执行到的语句 JVM也会报错





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