Menu
Topics Index
...
`

Throws keyword


If you need explanation Read this topic

If you need Answer Take test on this topic

Throws keyword
What will be the output of the following program?
public class City {
    class Manhattan {
        void doStuff() throws Exception {
            System.out.println("X ");
        }
    }
    class TimesSquares extends Manhattan {
        void doStuff() throws Exception {    }
    }
    public static void main(String[] args) {
        new City().go();
    }
    void go() throws Exception {
        new TimesSquares().doStuff();
    }
}

A. X
B. X X
C. (No output)
D. Compilation Error
E. 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.

D compilation Error

Posted by Prudhvi Raj    2014-02-27 03:54:38


Answer is d...new city().go() is an unreported exception..it must be caught or declared to be thrown..

Posted by Sneha Rapool    2014-02-27 04:22:19


In Java, there are two types of exceptions: checked and unchecked. If we call a method that can throw a checked exception, then we either need to catch the exception and handle it or declare that our method throws this exception. Failing to do so will result in a compilation error. Same is the case with go() method call here inside the main(). To fix the problem, either add a throws clause to the method declaration for the main (or) surround go() method call with a try/catch block. So, the answer is D.

Posted by Shaileshwar Sharma    2014-02-27 08:16:47


Congratulations Sneha Rapool. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-28 04:20:46


© meritcampus 2019

All Rights Reserved.

Open In App