Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Mon Sep 03, 2012 4:16 pm
by Karli
From the explanation:

"If you do either put a try catch block or declare a throws clause for the method then it will throw a NullPointerException at run time because e is null."

If the method is declared to throw Exception, then NPE will be thrown, but not necessarily for a try catch block:

try{
Exception e = null;
throw e;
}catch(Exception e) {System.out.println("hi");}

if exception e isn't used, no NPE is thrown

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Mon Sep 03, 2012 4:28 pm
by admin
That is indeed interesting because this is what the JLS 7 section 14.18 says:
A throw statement first evaluates the Expression. Then:
  • If evaluation of the Expression completes abruptly for some reason, then the throw completes abruptly for that reason.

    If evaluation of the Expression completes normally, producing a non-null value V, then the throw statement completes abruptly, the reason being a throw with value V.

    If evaluation of the Expression completes normally, producing a null value, then an instance V' of class NullPointerException is created and thrown instead of null. The throw statement then completes abruptly, the reason being a throw with value V'.
So it should throw NPE even if you are not using the exception and I think it does but your code will not show it because you are catching it and then not printing it :)

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Sat Feb 22, 2014 7:43 am
by prashantjain25
There is no error for

Code: Select all

throw null;
or

Code: Select all

Exception e=null; throw e;
without try-catch block but only exception so could you please correct this answer in question bank. :cry:

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Sat Feb 22, 2014 7:46 am
by admin
I am not sure what you mean to say. The question and explanation are correct.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Mon Feb 24, 2014 4:31 am
by prashantjain25
Question bank has option for both "error" and "exception" both but correct answer should be "exception" and not compile time error so even if we do not use try catch block generated "NullPointerException" will be handled by Exception class printing stack trace then and there and hence no compile time error will be produced.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Mon Feb 24, 2014 4:40 am
by admin
The explanation explains exactly why it will not compile. You might want to try it out :)

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Mon Feb 24, 2014 6:18 am
by prashantjain25
I ran the program compilation was fine but only NULLPointerException occurred which is a RuntimeException on Java 7 and since the outcome of the expression is null hence only exception should occur when throw terminates abruptly because it has null Value and this expression cannot be changed ever.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Mon Feb 24, 2014 10:06 am
by admin
Please type ( or just copy/paste) the code exactly as given. It doesn't compile for the reason given in the explanation.

thank you,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Fri Feb 28, 2014 8:07 am
by prashantjain25
I am attaching .class file which proves it is compiling :evil: and sry for uploading class file if Terms and Conditions are voilated but i think everyone can decompile and look for the content but, really had no other way to prove.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Fri Feb 28, 2014 8:15 am
by admin
Again, the given code cannot compile and the explanation given is correct.
Please copy the code from the file that you are compiling and paste it here.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Sat Mar 01, 2014 1:21 am
by prashantjain25

Code: Select all

package sample;

public class Test {
	public static void main(String[] ar){
	try{
		Exception e = null;
		throw e;
		}catch(Exception e) {System.out.println("hi");}
}
}

OUTPUT:
hi

This code is working fine with no errors only exception is occurred which is handled by JVM if you remove try-catch block and

Code: Select all

throw null;
directly it will compile PERFECT and first of all your explanation is ambigous look below to differentiate runtime and compile time errors
http://introcs.cs.princeton.edu/java/11 ... errors.pdf
and importantly over this debate below:-
http://www.coderanch.com/t/499993/java/ ... -exception
and Then Why so:-
http://docs.oracle.com/javase/7/docs/ap ... ption.html
NullPointerException under RuntimeException

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Sat Mar 01, 2014 2:17 am
by admin
Of course, the code that you've written will compile and that is why I asked you to paste the code that you are compiling.

But this is not the code that is given with the question. Look closely. There is no try/catch in the code given with the question.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Sat Mar 29, 2014 11:58 pm
by fasty23
Exception e = null;
throw e;
It needs try catch block because it throws an exception. But maybe my question is not related to this topic:
We throw a reference of Exception class that point to a null object, so we didn't throw an exception object but the compiler consider both an exception. And after we surround it in try catch block compiler realize that reference point to a null object so it throws a NullPointerException.
So why does the compiler behave reference same as object of the reference class while it (reference) point to null?

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Sun Mar 30, 2014 1:16 am
by admin
No, compiler doesn't throw NPE. The JVM does,at runtime.
Compiler doesnt care what actual object is e pointing to. It only cares about what is the declared type of e.

But the kind of questions you are asking, can you tell me which book are you following? Because you have some serious flaws in concepts and you need to go through a book.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Mon Jun 29, 2015 11:54 pm
by evafang2008
Yes, it does not compile. I copied the code to IDE, here is the result.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type Exception

at TestClass3.main(TestClass3.java:5)

Thanks for the explanation. Very helpful.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Tue Jun 30, 2015 12:27 am
by admin
Please avoid using IDEs for exam preparation. Use command line instead.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Wed Jul 28, 2021 6:35 am
by enthunoob
Great question and good explanation. I think it would be a little more clear if it is expanded with one word:

"You are throwing an exception and there is no try or catch block, or a throws clause. So it will not compile."

To

"You are throwing a checked exception and there is no try or catch block, or a throws clause. So it will not compile."

Even though the second part of the explanation sais this already, it would just connect better. Reading the fundamentals book I noticed this, leaving out the word checked or unchecked where it can be safely used, a lot actually and found it rather confusing.

It took me a while for example that, in case of this question, the fourth option would be correct if Exception e = null; would be replaced with RuntimeException e = null; as the latter is a unchecked exception and therefor doesn't require to be caught or a throws clause.

Re: About Question enthuware.ocajp.i.v7.2.1211 :

Posted: Wed Jul 28, 2021 6:47 am
by admin
Good point.
Added.
thank you for your feedback!