Page 1 of 1

About Question enthuware.ocajp.i.v8.2-.-1496 :

Posted: Tue May 26, 2020 4:52 pm
by swarna pakeer
option B says "RuntimeException and its subclasses are used for recoverable situation" and explanation in option C says "It is true that neither Errors nor RuntimeExceptions should be used for recoverable". could you please clarify which is correct.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed May 27, 2020 7:43 am
by admin
Option B is mistakenly set as the right option. Option C is the right option.
All the explanations are correct. i.e.
RuntimeExceptions such as NullPointerException, IndexOutOfBoundsException indicate that there is a coding error in the program. Ideally, a program should never encounter such exceptions. Therefore, if a program encounters a RuntimeException, it should be left unhandled. Instead of catching the exception, the code should be fixed and this exception should be eliminated.

Fixed.

thank you for your feedback.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed May 27, 2020 12:43 pm
by swarna pakeer
okay but i still see that B is marked as correct option.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed May 27, 2020 1:23 pm
by admin
It will be reflected in the updated question bank file.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed May 27, 2020 2:14 pm
by swarna pakeer
ok. and i see that Oracle documentation says "Exception is the superclass of all the exceptions from which ordinary programs may wish to recover.Error is the superclass of all the exceptions from which ordinary programs are not ordinarily expected to recover." did they mention anywhere saying Runtiime exceptions are not recoverable ? because as per above Runtime Exceptions may be recoverable.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed May 27, 2020 7:12 pm
by admin
From https://docs.oracle.com/javase/tutorial ... ntime.html
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed Jun 03, 2020 7:20 pm
by swarna pakeer
from the book "Hanuman Deshmukh" , it said " It is possible to recover from runtime exceptions but ideally, since they indicate bugs in the code, you should not attempt to catch them and recover from them. A well written program should not cause the JVM to throw runtime exceptions".

So above statement says Runtime Exceptions are recoverable right? Am really not able to decide if there is a situation where i need to choose one option among these two .A) Runtime Exceptions are not recoverable B) Errors are not recoverable.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed Jun 03, 2020 11:08 pm
by admin
The book is also correct. It is saying the same thing from another perspective. You can put use a try-catch to catch any RuntimeException or even an Error (obviously, because catch clauses takes a Throwable). So, it is technically valid. But should u do that? That is the issue.

Bottom line is this - runtime exceptions and errors are considered unrecoverable from a logical perspective. From the coding perspective, it is possible to recover from them by catching them but you should not do so.

If you catch, for example, ArrayIndexOutOfBoundsException, in your code, what are you going to do? You can't change the array at runtime. Ideally, the code should not have tried to access the array beyond the last element. So, it is really a coding issue. In that sense, an ArrayIndexOutOfBoundsException is not considered recoverable. But a novice programmer may add logic to their code in the catch(ArrayIndexOutOfBoundsException ae) block. It would be technically legal but is not the right thing to do.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed Jul 29, 2020 4:57 pm
by aevora
I think the answer is misleading.
Neither Errors nor RuntimeExceptions are used for recoverable situations. Both should be identified during testing and eliminated by fixing the code.
I agree with the first part but the second part made me choose a wrong answer because it seems to imply that all RuntimeExceptions and Errors are avoidable by fixing the code, and usually that is not the case. For example: An application is trying to call a Web Service or access a shared resource but the network is down.

I would suggest to modify the second part with something like:
Both should be identified during testing and eliminated, as much as possible, by fixing the code.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Wed Jul 29, 2020 10:04 pm
by admin
No, it is correct. In your example, if an application is trying to call a Web Service or access a shared resource but the network is down, then it should expect (and handle) a checked exception, not unchecked (i.e. Error/RuntimeException) exceptions.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Fri Jul 31, 2020 1:19 am
by aevora
Then think about an OutOfMemoryError because there are too many users using the application and the JVM has not enough memory available. My point is that Errors and RuntimeException are not always avoidable by fixing the code as the answer suggest.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Fri Jul 31, 2020 1:32 am
by admin
Errors are not fixable by code. They should not be caught because they indicate an issue with the environment.
RuntimeExceptions are fixable and therefore, should not be caught. Code should be fixed instead.

So, basically, the bottom line is that unchecked exceptions are not meant to be caught.

You can argue all you want about it but this is the official theoretical position. This is how you need to answer the question in the exam.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Fri Jul 31, 2020 3:16 am
by aevora
Errors are not fixable by code.
That's exactly what I am saying.

So, below, the word Both should be removed.
Neither Errors nor RuntimeExceptions are used for recoverable situations. Both should be identified during testing and eliminated by fixing the code.

Re: About Question enthuware.ocajp.i.v8.2.1496 :

Posted: Fri Jul 31, 2020 5:08 am
by admin
OK, got you! Sorry, I don't know what I was thinking.
-Paul.