About Question enthuware.ocajp.i.v7.2.842 :
Moderator: admin
-
- Site Admin
- Posts: 10058
- 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: 10058
- 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: 10058
- 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: 10058
- 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: 10058
- 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: 10058
- 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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
Updated.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Thu Dec 23, 2021 5:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
Please define "flow control". (https://en.wikipedia.org/wiki/Flow_control_(data) in the context of a programming language.Use flow control to terminate the loop
Did you mean "control flow statements"?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 24
- Joined: Wed Sep 28, 2022 9:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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".
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".
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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.
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.
-
- Posts: 8
- Joined: Tue Sep 19, 2023 12:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
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?
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.842 :
Thank you for your feedback. Will enhance.
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 58 guests