Mushfiq Mammadov wrote:
What happens? Compiler can’t determine that m2() method throw NullPointerException?
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
nick woodward wrote:
So if m2 threw and correctly handled a checked exception, would the catch block still be ok to not handle tc.m1()?
Also, Paul, are the test questions randomised? Pretty sure I had this question in Test 2, I got it right, but completely missed this 'level' of depth. I just assumed it wasn't handled correctly.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
Good question. You should try it out. Remember that having a throws clause does not mean that the method will always throw that exception. It just means that the method may throw any of the exceptions listed in the throws clause.
No, the questions are not randomized across standard tests. There are questions that look the same but are actually not. So I am pretty sure that you saw a different question in test 2 if you haven't taken test 5 yet.
nick woodward wrote:
Paul Anilprem wrote:
Good question. You should try it out. Remember that having a throws clause does not mean that the method will always throw that exception. It just means that the method may throw any of the exceptions listed in the throws clause.
hmmm. ok, so you mean that the compiler won't know if it is actually thrown.
but then why does explicitly throwing MyException in a try catch block within the finally clause also not work?
No, the questions are not randomized across standard tests. There are questions that look the same but are actually not. So I am pretty sure that you saw a different question in test 2 if you haven't taken test 5 yet.
Seems to be question 18/70 on Test 2 for me!![]()
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
nick woodward wrote:Hi Paul,
I meant by nesting another try/catch in the finally block which guarantees to throw a checked exception, my logic being that the compiler should know that finally definitely throws an exception:
but that doesn't work. I'm guessing because the compiler assumes that an exception might not be thrown because it's in a try block? Because this:
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Mushfiq Mammadov wrote:I thought D and E as correct answers
Mushfiq Mammadov wrote:I looked for the consistent part from book and I found it on page 312. I have taken a note the same part in my note file with a bit different form
Mushfiq Mammadov wrote:I understood that if we write tc.m2(); in finally block line 1 does not compile. But if write throw new NullPointerException(); instead of tc.m2(); the code will compile.
What happens? Compiler can’t determine that m2() method throw NullPointerException?
nick woodward wrote:RE the exception being forgotten - what OCA book are you using Mushfiq? I wouldn't mind going back over that if you have a reference.
nick woodward wrote:works fine, and seems much the same to me.
Roel De Nijs wrote:And finally (pun intended
), it's time for a bonus question. How can you make the original code snippet compile without handling or declaring MyException? Also removing and/or commenting lines is not allowed
I can think of (at least) 2 possible solutions. I have a cow on offer for the rancher who can list both
![]()
Good luck!
Kind regards,
Roel
nick woodward wrote:Ummm.... calling System.exit(); ? or a runtimeexception? both undoubtedly not what you meant, but still.....
nick woodward wrote:*edit: drop the catch clause?
nick woodward wrote:obviously the || part isn't real code
nick woodward wrote:heh
![]()
definitely cheating, but to spec
nick woodward wrote:(however on that last poing I'm a bit I'm a bit confused as to why the compiler doesn't know - I assume that even though the method which throws the exception is right there in the class, the compiler can't be sure if tc.m2() won't end up calling a method polymorphically? )
nick woodward wrote:I think what the compiler does or doesn't check frequently confuses me on things like this!
nick woodward wrote:I don't think so, because the compiler checks a.eat() based on the class, not the object type at runtime.
nick woodward wrote:I don't know though, I guess my confusion comes from the fact that I feel the compiler *should* be able to see in that code that 'a' is a new Lion, because it's explicitly stated - it isn't a situation where 'a' could be anything other than a lion, for example a = animalArr[x].
nick woodward wrote:I hope you understand where I'm coming from with this!
nick woodward wrote:I think I was just confusing the compiler's ability to check primitive assignments on the RHS
nick woodward wrote:yeah, although now looking at it i guess it's more similar to a switch expression and the case constants needing to be of the same type.
Consider Paul's rocket mass heater. |