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 2 of 3.
BhargavDave said:
9 years ago
What do for generate an exception?
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.
Muthu kumar said:
1 decade ago
How it will runs without catch ?
Deepak pathak said:
1 decade ago
Catch file is not define then this function is not present the finally.
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.
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.
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).
Sai Kumar said:
1 decade ago
return; means it's returning nothing.
So we can just give return;
So we can just give return;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers