Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling question

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A programmer wants to use this code in an application:


Which two are true? (Choose two.)
A. Class A will not compile.
B. Line 46 can throw the unchecked exception TestException.
C. Line 45 can throw the unchecked exception TestException.
D. Line 46 will compile if the enclosing method throws a TestException.
E. Line 46 will compile if enclosed in a try block, where TestException
is caught.

Here the answers are , D,E.
I can understand E is correct, as Line 46 should be enclosed in try{} block and the exception will be caught. Since the sayHello(String name) method in class A does not catch the TestException, so as line 46 calls the sayHello() method, the TestException should be caught there.

But i dont understand why the answer D is right. If enclosing method throws TestException, then how come Line 46 will compile?

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so as line 46 calls the sayHello() method, the TestException should be caught there.

Or propagate the exception, which is answer D. It would look like :



In this case, the exception will have to be caught(or propagated again) by the caller of this method.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic