Menu
Topics Index
...
`

try catch2971


If you need explanation Read this topic

If you need Answer Take test on this topic

try catch2971
What will be the output of the following program?
public class Valid {
    public static void main(String[] args) {
        try {
            throwException();
        } catch (Exception e) {
            System.out.println("Exception Handled in Main");
        }
        doesNotThrowException();
    }
    
    public static void throwException() {
        try {
            System.out.println("Method throwException");
            int k = 56 / 0;
        } catch (Exception e) {
            System.out.println("Handled in method throwException");
        }
    }
    public static void doesNotThrowException() {
        try {
            System.out.println("method doesNotThrowException");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

A. Method throwException
Handled in method throwException
B. Method throwException
Handled in method throwException
method doesNotThrowException

C. Some other output
D. Compilation Error or Runtime Error
Topic: Try Catch Block In Java

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 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 are calling the throwExection() fro main() here we are printing the Method
throwException and next we are diving the 56/0 so here it will casue the ArithemeticException and we are handling it..now it will enter into corresponding the catch block and prints the "Handled in method throwException"...so we didnt find any exeception in main so catch block in main() wont get executed...next we are calling the doesNotThrowException() so in this method we are not getting the any exception so here catch block wont execute...

Posted by Uday Kumar    2014-12-16 12:44:33


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. 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-17 07:02:10


© meritcampus 2019

All Rights Reserved.

Open In App