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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Karli

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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 :)
If you like our products and services, please help us by posting your review here.

prashantjain25
Posts: 9
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

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

Post 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:

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

I am not sure what you mean to say. The question and explanation are correct.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

prashantjain25
Posts: 9
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

The explanation explains exactly why it will not compile. You might want to try it out :)
If you like our products and services, please help us by posting your review here.

prashantjain25
Posts: 9
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

prashantjain25
Posts: 9
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

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

Post 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.
Attachments
Test.zip
just rename to ".class"
(640 Bytes) Downloaded 384 times

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

prashantjain25
Posts: 9
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

evafang2008
Posts: 9
Joined: Fri Jun 26, 2015 11:15 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Please avoid using IDEs for exam preparation. Use command line instead.
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Good point.
Added.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 47 guests