About Question enthuware.ocpjp.v8.2.1198 :
Moderator: admin
-
- Posts: 32
- Joined: Tue Oct 06, 2015 9:06 am
- Contact:
About Question enthuware.ocpjp.v8.2.1198 :
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
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
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1198 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Tue Oct 06, 2015 9:06 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1198 :
I agree! Thank you!
-
- Posts: 3
- Joined: Sun Jul 24, 2016 1:04 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1198 :
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.
> 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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1198 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Sun Sep 23, 2018 4:10 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1198 :
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.Martyjee wrote: ↑Wed Oct 14, 2015 3:58 amThe 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
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1198 :
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.
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 59 guests