• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptions doubt??  RSS feed

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

The following is from here

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 answers are given as 2 and 4, 4 is ok, but how come 2 and why not 5?? And will this ever compile??

Confusing.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jothi,
Please use the tag.It is very hard to read and understand the code.
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer is : 2 and 4.

Because, what ever exception is thrown or not, finally block will be executed. so, when it reaches finally block, it throws CloneNotSupportedException.
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jothi,

2 is fine as we have a catch block that catches IOException & the most important point to notice here that it's a

Checked Exception.Compiler mekes sure that Exceptin is thrown if you had a catch block for the same.

try{
} catch (IOException e){ }

code above will never compile as compiler will flag an error saying " exception java.io.IOException is never

thrown in body of corresponding try statement " . => No throw No catch
so to get your code compile you must have code like given below .

try{
throw newIOException ();
} catch (IOException e){ }

or call any method that migt throw IOException or it's SuperClass providing proper catch block .

again the Exception thrown in try block is overruled by Exception thrown in catch block and finally Exception

thrown in catch block is overruled by Exception thrown in finally block.
so referring said discussion we may conclude.

" If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException".

hope this will help.
Regards
Maneesh Saxena
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!