Java Programming - Exceptions - Discussion
Discussion Forum : Exceptions - Finding the output (Q.No. 6)
6.
What will be the output of the program?
public class Test
{
public static void aMethod() throws Exception
{
try /* Line 5 */
{
throw new Exception(); /* Line 7 */
}
finally /* Line 9 */
{
System.out.print("finally "); /* Line 11 */
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print("exception ");
}
System.out.print("finished"); /* Line 24 */
}
}
Answer: Option
Explanation:
This is what happens:
(1) The execution of the try block (line 5) completes abruptly because of the throw statement (line 7).
(2) The exception cannot be assigned to the parameter of any catch clause of the try statement therefore the finally block is executed (line 9) and "finally" is output (line 11).
(3) The finally block completes normally, and then the try statement completes abruptly because of the throw statement (line 7).
(4) The exception is propagated up the call stack and is caught by the catch in the main method (line 20). This prints "exception".
(5) Lastly program execution continues, because the exception has been caught, and "finished" is output (line 24).
Discussion:
18 comments Page 1 of 2.
Bhushan said:
2 decades ago
But every try block must followecd by catch block. ?
Sankyrahul said:
1 decade ago
Execution of finally must be followed by execution all catch block. ?
Sankyrahul said:
1 decade ago
The exception cannot be assigned to the parameter of any catch clause of the try statement ..why?
Manish said:
1 decade ago
When you throw exception the main function should also have throw.
Jagadeeshwar said:
1 decade ago
Try block must be followed by either catch block or finally or both. But it must be followed by atleast catch block or finally block.
Jim Bob said:
1 decade ago
I actually put this in netbeans and ran it under jdk 1.7 and got "exception finished".
Swas_99 said:
1 decade ago
Jim is right.
The output should be "exception finished" .
When an exception is thrown within a try block, a matching catch block is searched up the hierarchy(as long as code lies within try{}) -> first in the current method, then the calling method .. and so on. If no catch block is found till the last calling method = main(), control is transferred to OS to handle it appropriately.
The output should be "exception finished" .
When an exception is thrown within a try block, a matching catch block is searched up the hierarchy(as long as code lies within try{}) -> first in the current method, then the calling method .. and so on. If no catch block is found till the last calling method = main(), control is transferred to OS to handle it appropriately.
Rekha said:
1 decade ago
Please explain meaning of the statement "throw new Exception()" ?
Bisan said:
1 decade ago
I tried this code in Netbeans IDE under jdk1.8.0_11, the output is:
Finally exception finished.
Finally exception finished.
Shyam said:
1 decade ago
amethod() is valid or not because some program it shows the error.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers