Page 1 of 1

About Question enthuware.ocpjp.v8.2.1198 :

Posted: Wed Oct 14, 2015 3:58 am
by Martyjee
The answer marked as correct does not compile, because it is needed to catch an SQLException. For that reason that option is not good coding practice either.

In fact, none of the answers show "good" coding practice, because option 1 does not compile, option 2 does not compile, option 3 has an empty catch block, option 4 has a minor issue with closing resources.

I would change option 3 and let the catch-block print a stack trace or something else useful. Then mark that option as the right answer.

Kind regards,

Martijn

Re: About Question enthuware.ocpjp.v8.2.1198 :

Posted: Wed Oct 14, 2015 9:18 pm
by admin
The options do not show complete compilable code. They are just code fragments. By your logic, even putting a catch block will not make it compile because there is no method or class in which the code exists.

The fragment in option 1 is correct in itself. The exception can be declared in the encompassing method and is not relevant here. Further, whether you want to catch an exception or propagate it outside depends on your design. It doesn't necessarily become a good or bad coding practice if you do or do not catch an exception at its origination.

However, it is a matter of good coding practice to close the resources in the same place where they are opened and that is what this question is about.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1198 :

Posted: Thu Oct 15, 2015 4:27 am
by Martyjee
I agree! Thank you!

Re: About Question enthuware.ocpjp.v8.2.1198 :

Posted: Sun Jul 24, 2016 1:13 am
by JavaNerd
Option 4:

> While not an entirely bad practice…

It is an entirely bad practice. The code is prone to `NullPointerException`. If `connection.createStatement()` throws an exception, then `rs.close()` will throw an NPE in the finally block.

Re: About Question enthuware.ocpjp.v8.2.1198 :

Posted: Sun Jul 24, 2016 7:22 am
by admin
I don't agree with that. NPE is not necessarily a bad thing. In this case, it is certainly not. In fact, there is no point in catching NPE at this level because you can't do anything even if you catch NPE here. The whole point of having RuntimeExceptions is to reduce code clutter by avoiding useless catch blocks and throws clauses.

NPE usually signifies either a programming mistake (which should be eliminated before production) or a configuration mistake (which cannot be eliminated by any amount of code). In both the cases, all you can do is log it and either continue with the program (if you can) or exit.
-Paul.

Re: About Question enthuware.ocpjp.v8.2.1198 :

Posted: Sun Oct 28, 2018 12:21 pm
by floryan
Martyjee wrote:
Wed Oct 14, 2015 3:58 am
The answer marked as correct does not compile, because it is needed to catch an SQLException. For that reason that option is not good coding practice either.

In fact, none of the answers show "good" coding practice, because option 1 does not compile, option 2 does not compile, option 3 has an empty catch block, option 4 has a minor issue with closing resources.

I would change option 3 and let the catch-block print a stack trace or something else useful. Then mark that option as the right answer.

Kind regards,

Martijn
I agree, it asks for good coding practices and doesn't specify that it's about closing resources. And it is definitely a good coding practice to catch potential exceptions so I feel answer 3 (while still missing the handling of the exception in the catch block), is a better answer than the first.

Re: About Question enthuware.ocpjp.v8.2.1198 :

Posted: Sun Oct 28, 2018 9:12 pm
by admin
The problem statement and the options clearly indicate what you are expected to identity with respect to try-with-resource.
Option 3 is incorrect. See discussion above.

> And it is definitely a good coding practice to catch potential exceptions
No, as a blanket statement, it is incorrect. Again, go through the posts above.