About Question enthuware.ocajp.i.v7.2.1211 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1211 :
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
"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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
That is indeed interesting because this is what the JLS 7 section 14.18 says:
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 itA 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'.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Sat Feb 15, 2014 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
There is no error for or without try-catch block but only exception so could you please correct this answer in question bank.
Code: Select all
throw null;
Code: Select all
Exception e=null; throw e;
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
I am not sure what you mean to say. The question and explanation are correct.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Sat Feb 15, 2014 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
-
- Posts: 9
- Joined: Sat Feb 15, 2014 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
thank you,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Sat Feb 15, 2014 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
I am attaching .class file which proves it is compiling 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 393 times
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
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.
-
- Posts: 9
- Joined: Sat Feb 15, 2014 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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");}
}
}
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;
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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
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.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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?
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
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.
-
- Posts: 9
- Joined: Fri Jun 26, 2015 11:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
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.
"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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1211 :
Good point.
Added.
thank you for your feedback!
Added.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 113 guests