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 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
javabeat Question

Consider the following code snippet:

void m1() throws Exception{
try {
// line1
}
catch (IOException e) {
throw new SQLException();
} catch(SQLException e)
{ throw new InstantiationException();
} finally {
throw new CloneNotSupportedException() // this is not a RuntimeException.
}}

Which of the following statements are true? Select 2 correct options

(1) If IOException gets thrown at line1, then the whole method will end up throwing SQLException
(2) If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException
(3) If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException()
(4) If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException
(5) If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException()

The given answers are 2 and 4.

But when i run the program with line1 throwing IOException,i got the compile error at catch (SQLException e) . telling SQLException is never thrwon in try.

and also if i run the program with line1 thrown no exception,i got the compile error at catch (SQLException e) and catch (IOException e) .
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Siva,

Any Checked exceptions can't be thrown with out a try block or declaring method to "throws SQLException" .
But m1().. thrwing Exception right. Then there should be no problem with SQLException
[ March 29, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
If your try-catch block does not contain code, then you cannot catch any checked exception.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I think the main point of this example was to test your knowledge of what happens if the finally block includes a return or a throw.
[ March 29, 2007: Message edited by: Keith Lynn ]
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks to all for helping.this is the program,i an running




SQLException and IOException's are subclasses of Exception and the Exception is declared.Then why the error at line1 is coming?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I believe the creators of the question meant that the code could potentially throw a SQLException.

Again, the point of the question what not SQLException or IOException. It was to test to see if you understand what happens when you have a throw or return statement in a finally block.
[ March 29, 2007: Message edited by: Keith Lynn ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception