Question 73
Given:
11. static classA {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println(”B “); }
16. }
17. public static void main(String[] args) {
18.A a=new B();
19. a.process();
20.}
What is the result?
A. B
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
F. Compilation fails because of an error in line 19.
Answer: F
Question 74
Given:
11. static class A {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println(”B”); }
16. }
17. public static void main(String[] args) {
18. new B().process();
19. }
What is the result?
A. B
B. The code runs with no output.
C. Compilation fails because of an error in line 12.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
Answer: A
这两题不大懂,请分析一下

解决方案 »

  1.   

    第一题目 应该是静态方法吧   静态类??       静态方法 直接访问  第二个   new 的b   所以 a
      

  2.   

    B的process并没有覆写A的process所以第一题,process指的是A的void process() throws Exception
    而又没有catch这个exception所以,编译错第二题,process指的是B的void process()
    所以运行得到结果
      

  3.   

    throws Exception
    问题出在异常处理这个地方,Java中规定异常不能无限的往上抛,至少在最上层一定要做异常处理,也就是说a.process();需要加 try{}catch(...){}
    而类B中的process()方法没有抛出异常,所以正常执行
      

  4.   

    6楼正解,非runtimeexception是必须要try-catch的。
      

  5.   

    不try-catch就得在main函数添加throws。
      

  6.   

    看完这段代码才发现:
    B的process并没有覆写A的process 所以第一题,process指的是A的void process() throws Exception 
    而又没有catch这个exception所以,编译错 第二题,process指的是B的void process() 
    所以运行得到结果
      

  7.   

    第一题,没有try-catch;第二题,new 的是B当然选A
      

  8.   

    首先,这两个类都是内部类,而且都是静态的,不存在多态的问题
    第一个即使A a = new B();在调用a.process的时候仍然是调用A类中的方法 所以要处理异常
    第二个直接调用B类的方法 所以没有疑问