Java Programming - Exceptions - Discussion
Discussion Forum : Exceptions - Finding the output (Q.No. 1)
1.
What will be the output of the program?
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
Answer: Option
Explanation:
If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:
- An exception arising in the finally block itself.
- The death of the thread.
- The use of System.exit()
- Turning off the power to the CPU.
I suppose the last three could be classified as VM shutdown.
Discussion:
25 comments Page 1 of 3.
R.VINOD KUMAR said:
1 decade ago
Finally block is executing means releasing the resources used.
So always resources must be released after using, So Finally block must be executed compulsorly everytime.
If exception raises in try block, then JVM searches for Particular catch block, if catch block is absent then Finally will be executed to close the resources and then error/exception message will be printed on command prompt.
But here in this case no exception raised in try block. So, everything is fine (means simply Finally executed).
So always resources must be released after using, So Finally block must be executed compulsorly everytime.
If exception raises in try block, then JVM searches for Particular catch block, if catch block is absent then Finally will be executed to close the resources and then error/exception message will be printed on command prompt.
But here in this case no exception raised in try block. So, everything is fine (means simply Finally executed).
Av.nikhil said:
1 decade ago
The "return;" statement in try block has to send the control back to the calling method (main) without returning any value (because it allowed only in void methods). So without searching for catch blocks control going to the calling method.
But anyhow finally block should execute before the control goes the main method.
But anyhow finally block should execute before the control goes the main method.
P.vyshnavi said:
7 years ago
I think compilation fails because every try block having catch block is there but in this program, catch block is not there.
Correct me, if I am wrong.
Correct me, if I am wrong.
(1)
Parul said:
1 decade ago
The main method line is wrongly written (should be public static void main (String args[]) ) then how the code will be executed. No output will come.
Akhilesh said:
1 decade ago
If we use finally block it always executes anyhow to complete unfinished tasks. So here Finally will be printed on the output screen as a result.
Bhanu said:
7 years ago
Hi Parul,
Exchanging the place or syntax of the main method will not be affected for execution of program except void must be before main.
Exchanging the place or syntax of the main method will not be affected for execution of program except void must be before main.
Suraj Dhaigude said:
5 years ago
In the try block, no any exception so no need to catch block control direct finally method so it's print finally.
(1)
N.saikrishna said:
9 years ago
Speciality of finally block is it will be executed irrespective of the exception is raised or not raised.
Dheeraj Singh said:
10 years ago
System.exit(0) means process terminate abruptly and System.exit(1) means process terminate successfully.
Praveen.v said:
1 decade ago
A try can exist with out any catch block but catch can not exit with out try block.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers