Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your try-catch block does not contain code, then you cannot catch any checked exception.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic