About Question enthuware.ocajp.i.v7.2.842 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.842 :
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).
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.842 :
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);
}
}
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
You have an empty catch block. Put e.printStackTrace() in your catch block and you will see the exception.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.842 :
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".
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);
}
}
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.842 :
O.K., I see. You were pretty clear on that. Thanks!
-
- Posts: 1
- Joined: Sun Aug 25, 2013 4:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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".
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".
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Feb 20, 2014 3:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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)
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)
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri Jul 04, 2014 4:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
That's a fair point, Bluebox.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Fri Oct 03, 2014 2:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
I agree.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.
-
- Posts: 7
- Joined: Wed Nov 11, 2015 11:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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 -
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.
Code: Select all
for(int i = 0; i < arr.length; i++)
if(arr[i] < 100)
sum += arr[i];
-
- Posts: 5
- Joined: Wed Jun 07, 2017 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
Why is the first option not correct? It is one of the things that could be done independently.
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
-
- Posts: 29
- Joined: Sun Jul 16, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.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.
-
- Posts: 2
- Joined: Wed Jul 12, 2017 2:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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).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.
There is nothing to be logged here - you may as well be logging "sum calculated correctly" as logging the exception.
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
The question has been updated considering the feedback above.
thanks for the feedback!
thanks for the feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
-
- Posts: 9
- Joined: Mon Dec 14, 2020 8:53 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
"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.
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.
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
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.
-
- Posts: 13
- Joined: Mon Nov 18, 2019 4:59 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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:
As an argument I refer to SonarCube code checker S108 rule.
2. Question asks:
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;
}
2. Question asks:
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.Which of the following are best practices to improve this code?
Who is online
Users browsing this forum: No registered users and 200 guests