About Question enthuware.ocajp.i.v7.2.842 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

Best practices is valid here because there are multiple things involved. "Use flow control to terminate the loop." is a "best practice" and so is "Use ArrayIndexOutOfBoundsException for the catch argument and add code in the catch block to log or print the exception."

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

bluebox
Posts: 1
Joined: Fri Jul 04, 2014 4:56 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by bluebox »

I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

That's a fair point, Bluebox.
If you like our products and services, please help us by posting your review here.

FruityPebbles
Posts: 3
Joined: Fri Oct 03, 2014 2:29 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by FruityPebbles »

bluebox wrote:I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.
I agree.

prasanna_ls
Posts: 7
Joined: Wed Nov 11, 2015 11:59 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by prasanna_ls »

Since, 5th option is correct, and let's say the programmer does use flow control. That would mean that he would replace the while loop with a for loop(or a while loop which acts similar to a for loop). Since he is taking the extra measure to make sure that the code doesn't go outside the range of the array, why would he want to change the catch argument to ArrayIndexOutOfBoundsException? That's never going to happen given a code like this -

Code: Select all

for(int i = 0; i < arr.length; i++)
    if(arr[i] < 100)
        sum += arr[i];
That's never going to throw an ArrayIndexOutOfBoundsException since we're using flow control. So wouldn't option 3("Add code in catch block to handle the exception") be more appropriate? That code could, for example, throw a NullPointerException.

yrelhan
Posts: 5
Joined: Wed Jun 07, 2017 3:13 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by yrelhan »

Why is the first option not correct? It is one of the things that could be done independently.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

Because between option 1 and 2, option 2 is better and you have to select 2 correct options only.
If you like our products and services, please help us by posting your review here.

philippe
Posts: 29
Joined: Sun Jul 16, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by philippe »

bluebox wrote:I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.
I Think bluebox makes a good point here. It's a good practice to let a user know about exceptions that occur, at least when these exceptions are used for what they were invented for. In this case, exceptions are (mis)used to stop the iteration over the int array. Telling the user of the processArray method that the iteration had stopped would be undesirable for that user.

redvee78
Posts: 2
Joined: Wed Jul 12, 2017 2:38 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by redvee78 »

bluebox wrote:I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.
This is the correct answer in my opinion. There is no reason at all to log the exception because it is 100% expected, and the code does the right thing (albeit we all can agree it's a poor use of loops/exceptions).

There is nothing to be logged here - you may as well be logging "sum calculated correctly" as logging the exception.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

The question has been updated considering the feedback above.

thanks for the feedback!
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by crazymind »

Which one is the answer? I mean Option 2 is definitely a best practice. It is kind confusing to compare one best practice with another best practice. I don't get this one. Does real exam has this kinda of question? Thanks.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

The given answer is correct. Yes, it is possible to get such question in the exam where you have to select best option. Please go through the given explanations carefully.
If you like our products and services, please help us by posting your review here.

fortesp
Posts: 9
Joined: Mon Dec 14, 2020 8:53 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by fortesp »

"Empty catch blocks are generally a bad practice because at run time, if the exception is thrown, the program will not show any sign of the exception and may produce bad results that will be hard to debug. Therefore, it is a good practice to at least print out the exception if you don't want to do any thing upon encountering an exception.

However, in this case, since the code is deliberaly written such a way that an exception will be thrown, the timing and cause of the exception are already known. Therefore, there is no need for logging the exception."


Hi,
Isn't this explanation a bit contradictory? "..the code is deliberaly written such a way.." in what way exactly? This does not make sense to me.
Do you have any official reference where this topic is touched?

Cheers.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

No, it is not contradictory.

First the general principle is explained that empty catch blocks are not a good idea. Then an exception to the general principle is explained. This particular code relies on the exception being thrown. So, the developer already expects the exception and already knows the cause of the exception. So, there is no need to print it out.

Not sure what you mean by "official" reference. Even "good" books don't cover all the material. You may post this on other forums and get a second opinion.
If you like our products and services, please help us by posting your review here.

pavel.gurinovich
Posts: 13
Joined: Mon Nov 18, 2019 4:59 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by pavel.gurinovich »

I completely understand explanation for this question but cannot agree with it, so I want to give my feedback.

1. Empty catch block it is always a bad practice. You should leave at least comment something like this:

Code: Select all

public static Integer parseIntegerSafe(String string){
    Integer result = null;
    try {
        result = Integer.valueOf(string);
    } catch (NumberFormatException e) { /* ignore */}
    return result;
}
As an argument I refer to SonarCube code checker S108 rule.

2. Question asks:
Which of the following are best practices to improve this code?
Phrase best practices is not appropriate for any try/catch solution in given example. There should be used phrase better practices or even slightly better practices.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

Updated.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

ignore.your
Posts: 3
Joined: Thu Dec 23, 2021 5:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by ignore.your »

Use flow control to terminate the loop
Please define "flow control". (https://en.wikipedia.org/wiki/Flow_control_(data) in the context of a programming language.

Did you mean "control flow statements"?

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

You are right. It should be control flow statements. The option statement has been updated to "Use control flow statements to terminate the loop."

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

edufin166@yahoo.com
Posts: 24
Joined: Wed Sep 28, 2022 9:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by edufin166@yahoo.com »

Im my oppinnion, the answer corret is "Use ArrayIndexOutOfBoundsException for the catch argument and add code in the catch block to log or print the exception".

Even though, the reason explained is that : "However, in this case, since the code is deliberaly written such a way that an exception will be thrown, the timing and cause of the exception are already known. Therefore, there is no need for logging the exception"

The question ask EXPLICITLY "best practices to improve this code".

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

It is indeed a good practice to log an exception at a minimum. But this practice is for exceptional situations. i.e. where you would want to check the logs if an exception is thrown so that you will have more information for fixing/improving the code. In the given situation, there is no such need because you know that the exception will be thrown, you know why that exception will be throw, and you want the exception to be thrown.

Best practices are not to be applied blindly. You need to understand why that practice is called a best practice and the reason for doing it.
If you like our products and services, please help us by posting your review here.

powerofviva
Posts: 8
Joined: Tue Sep 19, 2023 12:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by powerofviva »

I really dont understand why the first answer is better than the second one.
It is always better to add the code in catch block and log it.
it is always bad practice to have empty catch block, how can here say just for the exam, the answer 1 is correct, but the second is not!

#1 is correct, but #2 is always best practice, maybe you should change the answer?

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.842 :

Post by admin »

Thank you for your feedback. Will enhance.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests