Welcome Guest | Sign in | Register
Exceptions - Discussion Page For Q.2611 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Exceptions > Questions and Answers

Q: class A{
public static void main(String[] args) {
try {
args = null;
args[0] = "test";
System.out.println(args[0]);
} catch (Exception ex) {
System.out.println("Exception");
} catch (NullPointerException npe) {
System.out.println("NullPointerException");
}
}
What is the result?
A. test B. Exception
C. Compilation fails. D. NullPointerException

Answer and Explanation

Answer:Compilation fails.

Explanation:
Whenever you write number of catch blocks, catch block with super class (Exception)should be always at the end. Because if you write first then rest of the catch blocks will not going to execute. If you write super class Exception first then rest of the exception type next then it will throw compile time error. So in our program NullPointerException should come first then Exception class.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.