Menu
Topics Index
...
`

Finally keyword


If you need explanation Read this topic

If you need Answer Take test on this topic

Finally keyword
What will be the output of the following program?
public class FinallyDemo {
    FinallyDemo(int x, int y) {
        try {
            System.out.println("Division result is " + x / y);
        } catch (ArithmeticException ae) {
            System.out.println("Cannot divide by zero");
        } finally {
            System.out.println("Finally Executed");
        }
    }
    public static void main(String[] arg) {
        new FinallyDemo(10, 0);
    }
}

A. Cannot divide by zero
Finally Executed

B. Division result is 0
Finally Executed
C. Cannot divide by zero
D. Finally Executed
Topic: Java Finally Block In Exception Handling

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 Exception concept..Exception is an Runtime error which cause the
abnormal termination of the program...when the problem occur...jvm collects the information
about the problem and thrwos to the programmer...if we handle it than we can avoid the abnormal
termination..we can handle it by using the try,catch,throws,finaly..
Execution:

here we have the Constructor which can tak 2 arguments..from main() we are creating the instance for the FinallyDemo by passing the 10,0 as arguments..than the constructor get invoked and there we are dividing the by 0 so exception will occur...than we are handling it using catch so catch get executed so it prints "Cannot divide by zero"
next we have finally block which executed always..irrespective of the Exception...this is mainly used for the Connection closing purpose...even though if we have return in the catch control will come to the finally...

Posted by Uday Kumar    2014-12-17 04:06:20


A

Posted by Arockia Raj    2014-12-17 12:45:04


ans a . Java try block is used to enclose the code that might throw an exception. It must be used within the method. Java try block must be followed by either catch or finally block.   finally block, if used, is placed after a try block and the catch blocks that follow it. The finally block contains code that will be run whether or not an exception is thrown in a try block.here we created Finally demo object with passing 10,0 arguments. in try block we tried to divide 10/0 ,which is an illegal arithmetic operation so try block throw exception,here we have catch block with arithmetic Exception handler so catch block get executed. sop get printd.thn control goes into finally block and get executed, sop printd.finally block always get executed

Posted by Maheshwari Natarajan    2014-12-17 19:18:40


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 Bolo Raven. 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-12-18 04:25:43


© meritcampus 2019

All Rights Reserved.

Open In App