Page 1 of 2

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

Posted: Fri Mar 01, 2013 2:14 pm
by ETS User
I can't see how question 3 would throw an ArrayIndexOutOfBoundsException using while(values<100) as values will never be more than the length of the array. Should it not be (values.length < 100).

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

Posted: Fri Mar 01, 2013 2:52 pm
by admin
The program will throw ArrayIndexOutOfBoundsException every time if all the values in the array are < 100, because it does not check the length of the array. It just keeps trying to access values.

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

Posted: Mon Mar 11, 2013 3:00 pm
by vclortho
Hello. I am not able to get an ArrayIndexOutOfBoundsException with the below code and several variations I have tried. Please let me know your thoughts. Thanks in advance for your help.

Code: Select all

public class TestClass {


		public static void processArray(int[] values){
	        int sum = 0;
	        int i = 0;
	        try{
	            while(values[i]<100){
	                sum = sum +values[i];
	                i++;
	            }
	        }
	        catch(Exception e){ }
	        System.out.println("sum = "+sum);
				}
		
		public static void main(String[] args) {
		int[] testArray = {6,9,14,7,8,96,678,246,15};
		processArray(testArray);
		}
}

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

Posted: Mon Mar 11, 2013 3:10 pm
by admin
You have an empty catch block. Put e.printStackTrace() in your catch block and you will see the exception.

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

Posted: Mon Mar 11, 2013 3:20 pm
by Guest
Thanks for responding so quickly. I did as you said, but I'm still not seeing it. I must be missing something else.

The result of running the code is "sum = 140".

Code: Select all

public class TestClass {


		public static void processArray(int[] values){
	        int sum = 0;
	        int i = 0;
	        try{
	            while(values[i]<100){
	                sum = sum +values[i];
	                i++;
	            }
	        }
	        catch(Exception e){e.printStackTrace();}
	        System.out.println("sum = "+sum);
				}
		
		public static void main(String[] args) {
		int[] testArray = {6,9,14,7,8,96,678,246,15};
		processArray(testArray);
		}
}

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

Posted: Mon Mar 11, 2013 4:26 pm
by admin
Please see the message posted above:
admin wrote:The program will throw ArrayIndexOutOfBoundsException every time if all the values in the array are < 100, because it does not check the length of the array. It just keeps trying to access values.

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

Posted: Mon Mar 11, 2013 5:17 pm
by Guest
O.K., I see. You were pretty clear on that. Thanks!

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

Posted: Sun Aug 25, 2013 5:02 am
by Jellybean
The text "sums all the integers that are less than 100" is incorrect.

If you have an array:

int[] testArray = {6,9,14,700,8,96,678,246,15};

Then the while loop will end when it gets to 700, and not carry on adding any of the other numbers less than 100.

It should say "sums all the integers that are less than 100 until an integer of 100 or more is encountered".

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

Posted: Sun Aug 25, 2013 6:30 am
by admin
Yes, but that is ok because
1. the programmer is a new java programmer -
2. the answer to the question does not depend on the the intention of the programmer with respect to how he wants to handle input that is more than 100. i.e. whether he wants to continue with the rest of the numbers or end the processing as soon as he gets a number > 100.

HTH,
Paul.

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

Posted: Thu Feb 20, 2014 3:18 pm
by dscarrol
A correct answer was: "Use flow control to terminate the loop."

Part of the reason given for this was: "It is considered bad practice to use exceptions to control the flow of execution."

Given that reasoning, all other options are incorrect answers (i.e. they are considered bad practice, not "best practice" as the question asked for)

:)

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

Posted: Thu Feb 20, 2014 8:41 pm
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.

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

Posted: Fri Jul 04, 2014 5:01 am
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.

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

Posted: Fri Jul 04, 2014 5:14 am
by admin
That's a fair point, Bluebox.

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

Posted: Mon Oct 06, 2014 9:21 am
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.

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

Posted: Wed Nov 25, 2015 10:10 am
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.

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

Posted: Thu Jun 15, 2017 2:22 pm
by yrelhan
Why is the first option not correct? It is one of the things that could be done independently.

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

Posted: Thu Jun 15, 2017 9:49 pm
by admin
Because between option 1 and 2, option 2 is better and you have to select 2 correct options only.

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

Posted: Mon Jul 31, 2017 3:54 pm
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.

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

Posted: Tue Aug 01, 2017 8:07 am
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.

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

Posted: Tue Aug 01, 2017 8:31 pm
by admin
The question has been updated considering the feedback above.

thanks for the feedback!

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

Posted: Thu Jan 10, 2019 4:42 pm
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.

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

Posted: Thu Jan 10, 2019 11:55 pm
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.

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

Posted: Sat Jan 09, 2021 7:42 am
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.

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

Posted: Sat Jan 09, 2021 12:34 pm
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.

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

Posted: Mon Apr 19, 2021 11:05 am
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.