• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in exception ques?

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOURCE:www.javabeat.net


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()


answer is:2,4...

how is it possible?

i compiled this code with different options..only option-1 is correct..

am i right?

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your "line 1" is the m1() call in main.

Assuming so, if IOException is thrown, then the first catch clause is eligible to catch it, but there is a finally clause also with a return statement. If there is a return statement in finally clause, then any other return is neglected...ie. the finally takes precedence and finally's return statement is the one which takes effect.

So, option (1) would not be correct.
2 is correct because -- Same explanation as given above
4 is correct because -- finally gets executed no matter whether catch is executed or not and no matter whether exception is thrown or not.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if we throw IOException in line-1 then compilation error as SQLException is not thrown

In other case compiler shows an error as IOException is not thrown...

i compiled this code....
[ December 04, 2008: Message edited by: Ganeshkumar cheekati ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ganesh

This problem is similar to your other post
https://coderanch.com/t/418092/java-programmer-SCJP/certification/checked-exception

Also, for the code you posted, I dont think the options are right with the code AS-IS...
I tried the following modified code... And with this code, options 2 and 4 are correct.



Line marked Comment-1 : Uncomment this line alone, and comment out the catch(SQLException e) block... What happens is, IOException is thrown, catch (IOException e) is eligible to catch it, buy finally overrides the return with CloneNotSupportedException. Hence, CloneNotSupportedException is thrown at the end.

Line marked Comment-2 : Uncomment this line alone...This calls another function m2() that declares that it COULD throw an Exception but actually it does not ...
In this case, since no exception is thrown, no catch is run but finally runs thus throwing a CloneNotSupportedException.

I feel the options given are not suitable ones for the original code because there are compilation errors. Sorry, I didnt compile the code earlier.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest to go through JLS which will explain this

Always remember that whatever exceptions are thrown before finally block
are ignored or discarded if finally throws any exception and reason for that exception will be consider for abrupt completion of finally block
at runtime

I hope this clears
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This clears veeery fine to me Ninad Kulkarni. Tks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic