• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Exceptions doubt??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jothi,
Please use the tag.It is very hard to read and understand the code.
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Why is the word "abbreviation" so long? And this ad is so short?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic