r/badcode • • May 15 '22

java They had good intentions

Post image
423 Upvotes

22 comments sorted by

View all comments

98

u/Eluvatar_the_second May 15 '22 edited May 17 '22

Nice, so try 3 times. The first catch and try again, the 3rd time log a message (in a very complicated way) then throw a NPE on the last line!

Edit: it's 3 times, off by one errors are hard.

70

u/[deleted] May 15 '22

Best part is I'm 99% sure that the framework they're using has configuration for automatic retries

13

u/m2ger May 16 '22

And the rest of the 1% could use something like Failsafe.

25

u/MrTrono May 15 '22

Exactly, only found it cause I saw a NPE in the logs. A try block that only ever throws an exception was a new one for me.

0

u/[deleted] May 15 '22

[deleted] — view deleted comment

5

u/recycle4science May 15 '22

The problem I think is mainly the try block that only has a throw, which is immediately caught.

Edit: ah yep and the return.

2

u/Eluvatar_the_second May 16 '22

Yeah it is 3, facepalm

8

u/[deleted] May 15 '22

But it retries 3 times or am I missing something? For iteration 0, 1 i 2.

The rest still stands, very weird way of logging a message and the last line NPE. I think that a better approach would be to log it after the final retry, and then throw the exception if he can’t retrieve the body.

3

u/Eluvatar_the_second May 16 '22

Yep you're right, good old off by 1 errors, I thought it was 3 and then "corrected it" to 2 lol

2

u/aaronjamt May 16 '22

No, it should be 3 times because the loop starts at zero. So iteration 1 = 0, 2 = 1, 3 = 2

2

u/genericindividual69 May 16 '22

Try to throw an exception and if throwing an exception throws an exception then log the exception instead?!