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()
how is it possible?
i compiled this code with different options..only option-1 is correct..
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.
posted 11 years ago
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 ]
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.
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