File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception handling question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception handling question" Watch "Exception handling question" New topic
Author

Exception handling question

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115

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?


SCJP 6.0 90%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
thanks, got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling question
 
Similar Threads
Unreported exception Problem
Concept on coding for Exception Handling
Exceptions - Basics
methods throwing exceptions!!!!!!