Menu
Topics Index
...
`

Program with try catch blocks2967


If you need explanation Read this topic

If you need Answer Take test on this topic

Program with try catch blocks2967
public class Exception4 {
    void m() {
    }
    void n()  {
        m();
    }
    void p() {
        try {
            n();
        } catch (Exception e) {
            System.out.println("exception handled");
        }
    }
    public static void main(String args[]) {
        Exception4 obj = new Exception4();
        obj.p();
        System.out.println("normal flow");
    }
}

A. exception handled
normal flow
B. normal flow
C. Some other output
D. Compilation Error or Runtime Error
Topic: Exception Handling In Java with Example Program

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 B..here we have the Exceptions Concept...Exception is an Runtime error which
causes the abnormal termination of the Program...when Problem occurs JVM will
collect the information about the problem as Object and throws to the programmer if
we handle than we can avoid the abnormal termination..for handling purpose we use some keywords try,catch,throws,Finally..

Execution:

here we create the Instance for the class and we invoked the method p..from method we are calling the method  n() we put these calling statement in the try-catch block..if something goes wrong than we are handling it in the catch..so control goes into the method n() from there we are calling the m() we are not doing anything in the m returns back to n and backs to the p() so here No exception is raised so Catch wont get executed...and backs to main() and prints normal flow

Posted by Uday Kumar    2014-12-26 04:09:01


ans b.
A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code.

coming to this program initially Exception4 object is created.
thn function p() called by using obj reference.

p() -here try catch blocks used to handle run time exception ,here 1st control goes inside try block,it calls n(),here no exception thrown so catch block not get executed.

n() this method called m()

m() has empty body
finally sop prints "normal flow"

Posted by Maheshwari Natarajan    2014-12-26 04:20:59


ANS IS B Output: Exception Handled

Try catch block is used to handle the exception(Run time error in program caused due to wrong logic in program) which when occur catch block is executed to take corrective action for the exception occurred.

IN Main()
--Object of Exception4 is created.Using this object method p() is called.

--In method p() Try catch block is used.In try block method n() is called inside which method m() is called.As method m() is empty i.e. No exception has occured so CATCH will not be executed.

--After that "NORMAL FLOW" will get printed.

--Method p() calls method n() which in turn calls method m() and empty method m() does not throw any exception. Thats why No corrective action is required. "Execption handled" will not get printed.

Posted by Mânïshå Mùlchåndânï    2014-12-26 10:19:16


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 Asma Mujtaba Khan. 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-29 04:58:26


© meritcampus 2019

All Rights Reserved.

Open In App