Menu
Topics Index
...
`

try catch and finally blocks1582


If you need explanation Read this topic

If you need Answer Take test on this topic

try catch and finally blocks1582
What will be the output of the following program?
public class GetExceptions {
    public static void main(String[] args) {
        try {
            throwException();
        } catch (Exception e) {
            System.out.println("Exception Handled in Main");
        }
        doesNotThrowException();
    }
    public static void throwException() throws Exception {
        try {
            System.out.println("Method throwException");
            throw new Exception();
        } catch (Exception e) {
            System.out.println("Handled in method throwException");
            throw e;
        } finally {    System.out.println("Finally is Always Exceuted ");    }
    }
    public static void doesNotThrowException() {
        try {    System.out.println("method doesNotThrowException");    }
        catch (Exception e) {    System.out.println(e.toString());    }
        finally {    System.out.print("Finally is always Executed.");    }
    }
}

A. Method throwException
Handled in method throwException
Finally is Always Exceuted 
Exception Handled in Main
method doesNotThrowException
Finally is always Executed.
B. method doesNotThrowException
Finally is always Executed.
C. Handled in method throwException
Finally is Always Exceuted 
method doesNotThrowException
Finally is always Executed.
Exception Handled in Main
D. Finally is Always Exceuted 
method doesNotThrowException
Finally is always Executed.
E. Output cannot be determined
F. Compilation Error or Runtime Error
Topic: Java Throw Keyword - Java Throws Keyword

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:A
intially it starts from main methos,there it executes
throwexception() method
when it enters this method it prints
"method throwException"
and throws a exception.as there is a catch block it enters there and executes statements in it
so it prints"Handled in method throwException"
then now it enters into finally block executes its statements.so it prints
"Finally is Alwasys Executed"
now it returned to main,as we had a throw e in last method it is catched now and so it executes the statements of catch
block of main method so it prits
"Exception Handled in main"
now it calls
doesNotThrowException()
in that mehtod it prints
"method doesNotThrowException"
as there is no throw it doesnot enters into catch block but enters into finally
block and executes its statements.so it prints
"Finally is always Executed"

Posted by Varun Kumar    2014-10-31 13:05:30


Ans is A..here we have Exceptions Key word..here we are using 5keywords of exception...
try--> the statements which may cause Exceptions wil put in try{} Block...
catch--> is used to Handle the Exceptions which was thrown by the try...evry try block must be followed by the one or more catch blocks..
throw---> is an keyword which is used to throw the Exceptions by the programmer manulaly

throws--> is key word in java which is ued to indicate the system that Exceptions raied in this method defination wil not handled here..the caller of the method should handle this

finally---> is block is used after the try catch mechanisam..which wil always execute...this wil used mainly for DB connection closing purpose..

here in the main we are calling the throwException() in that method Printing Method throwException... there we are throwing "Exception" so that it wil enter into Following catch and there it wil print "Handled in method throwException"
there we are again throwing Exceotion which is received from the try block..

so that finally block is executed...and it wil print "finally is always executed"

next we are calling doesNotThorwException() in this method we are not throwing any exception...so that Corresponding catch block but the finaly block wil get executed...Bcz it won Depends On Exceptions..it is always executed

Posted by Uday Kumar    2014-10-31 16:21:00


A

Posted by Shubham Bansal    2014-10-31 17:04:52


ans a.     in this program exception handling mechanism explains usage of try,catch and finally block. mainly it shows catch and finally block difference. catch block get executed only if try block throws exception.but finally block always get executed if it is included inside the exception handling method because finally block is an optional .exception is thrown using throw and throws.                                           if function need exception handlin declaration thn it can use throws keyword followed by function name .this method is used to runtime unchecked exceptions.for checked exception throw keyword used.             in this program main methods control goes inside try block,it calls throeException() function. this function declared with throws.inside this function try block executed sop get printed.thn exception thrown thn catch block get executed aft this finally block executed.finally block always get executed.here catch block throw exception this is handled by main catch block.                                                                                                   doesNotThrowException() function called.here try block dont throw any exception bcz it ve ly sop.so catch block not excuted.finally block executed and prints sop.

Posted by Maheshwari Natarajan    2014-10-31 18:03:41


Ans is A

here we are dealing with Exception Handling mechanism in the above programm
in the GetExceptions class in the main method

try block is executed where here and user defined method is called named as "throwException"
where the control goes to that method and

enters the try block of that method and prints the SOP statement as

i.e......//Method throwException

then the next statement is the user knownigly thrown Exception so the Exception is thrown from the try block where it checks for the suitable catch clause

when it meets the suitable catch block the catch block takes it and handles it likewise the same is done here also the Exception thrown is taken into the catch block and handled it and
prints the SOP statement in the catch block

i.e.. ///Handled in method throwException

as it is said finally block i.e..what ever may be the reason abnormal or normal termination of the program finally definately gets executed
so here it prints SOP statement

i.e...//Finally is Always Executed

and again the control goes back to the place from where it is called since the Exception is raised in the try block the corresponding catch handles it and prints the SOP Statement i.e...//Exception handled in main

and then normally the last statement of the main method is executed i.e..doesNotThrowException() method is called
the controls is transferred there

the try block executes and prints SOP statement

i.e...//method doesNotThrowExecption

and since no Exception raised here normally finally block is executed

i.e..//Finally is always Executed

Posted by Vicky Maduri    2014-11-01 02:16:50


This dose is now closed and the winners are Varun Kumar,  for 'First Correct Comment', Vicky Maduri,  for 'Best Comment' and Šâí Râm 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-11-03 01:36:58


© meritcampus 2019

All Rights Reserved.

Open In App