Menu
Topics Index
...
`

Final


If you need explanation Read this topic

If you need Answer Take test on this topic

Final
What will be the output of the following program?
public class Butterfly
{  
    public static void main(String[] args)
    {
        try
        {
            return;
        }

        finally
        {
            System.out.println("Finally");
        }
    }
}

A. Finally
B. Compilation Error
C. The program executes but there is no output.
D. An exception is thrown at runtime.
Topic: Exception Handling Syntax In Java Programming

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A...here we have the try, finally...

try is block which is used to put the staments which may cause the Exception...exception is Runtime error which will cause the abnormal termination of the program...when the Problem occur system will gather info about the problem and throws that obejct to the programmer if the programmer that object using catch than we can avoid the abnoraml termaintion if not program will terminate..we cant say the if we have try-catch we can avoid termination..we must handle it properly...

every try block must contain either catch (or) finally....every try can contain any num of catch blocks but only on finally block...and it must be the last..

Finally: is is an block which will executes always even though exception is not thrown.....if we have catch blocks to the try than finally block must be the last..we cant have any statements bw the try and finally block..even though we have return statements in the try catch block before returning the control will comes to the finally...it is mainly used to clean up processs like DB Conncetions closing....

if we use the finally instead of the catch we cant avoid the Exceptions but we can execute some code before Termination...

here we have return statement in the try block....here it will return after control goes to finally than it will execute code in that block and prints "Finally"..

Posted by Uday Kumar    2014-11-21 12:47:52


ans a. Its focus is the manner in which the JVM handles finally clauses and the bytecodes that are relevant to these clauses.                                                               As the Java virtual machine executes the bytecodes that represent a Java program, it may exit a block of code -- the statements between two matching curly braces -- in one of several ways. For one, the JVM simply could execute past the closing curly brace of the block of code. Or, it could encounter a break, continue, or return statement that causes it to jump out of the block of code from somewhere in the middle of the block. Finally, an exception could be thrown that causes the JVM either to jump to a matching catch clause, or, if there isn't a matching catch clause, to terminate the thread. With these potential exit points existing within a single block of code, it is desirable to have an easy way to express that something happened no matter how a block of code is exited. In Java, such a desire is expressed with a try-finally clause.                                                                                                
At a basic level catch and finally solve two related but different problems:
catch is used to handle a problem that was reported by code you called
finally is used to clean up data/resources that the current code created/modified, no matter if a problem occurred or not
So both are related somehow to problems (exceptions), but that's pretty much all they have in common.
An important difference is that the finally block must be in the same method where the resources got created (to avoid resource leaks) and can't be put on a different level in the call stack.                                                                            The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.                    finally should have atleast a try block, catch is optional. The point of finally blocks is to make sure stuff gets cleaned up whether an exception is thrown or not .                                                                                                                           so bcz of these reasons this progrm works properly,sop prints finally

Posted by Maheshwari Natarajan    2014-11-21 18:21:54


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gaurav Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-23 02:24:56


© meritcampus 2019

All Rights Reserved.

Open In App