×

Welcome to the Slashdot Beta site -- learn more here. Use the link in the footer or click here to return to the Classic version of Slashdot.

Thank you!

Before you choose to head back to the Classic look of the site, we'd appreciate it if you share your thoughts on the Beta; your feedback is what drives our ongoing development.

Beta is different and we value you taking the time to try it out. Please take a look at the changes we've made in Beta and  learn more about it. Thanks for reading, and for making the site better!

[Java] You know when you are knee deep in crap code....

FortKnox (169099) writes | more than 7 years ago

User Journal 16

I should submit this to the daily wtf...
You know you're knee deep in crap code when you see this in production:

// ...
} // end try
catch(ArrayIndexOutOfBoundsException e)
{
}

I should submit this to the daily wtf...
You know you're knee deep in crap code when you see this in production:

// ...
} // end try
catch(ArrayIndexOutOfBoundsException e)
{
}

I'm one of those that hates swallowing exceptions (yeah, there are rare exceptions to that rule), but to catch an array index out of bounds exception... and then swallow it? Gah!

16 comments

What? (1)

dthable (163749) | more than 7 years ago | (#15890945)

I always code the main part like:

public static final int main() {
    try {
    } catch( Exception e) {
    }
}

Why do I want to see all that junk on my screen?

Could be worse... (1)

rk (6314) | more than 7 years ago | (#15890972)

catch (Exception e) {

}
Not sure if that would actually work, not being a Java guy, but you get the point. :-)

Re:Could be worse... (1)

FortKnox (169099) | more than 7 years ago | (#15891067)

That's not bad. In java, you can catch multiple exceptions, and put in a 'just in case' sort of thing, like so:
try
{
//...
}
catch(BadInputException bie)
{
//do something
}
catch(IOException ioe)
{
//do something else
}
catch(SQLException sqle)
{
//do something else
}
catch(Exception e)
{
//just in case another (new?) exception passed us by
}

In Java, Exception is the root object in the exception inheritence tree, but catches go in order so you catch everything you are prepared to handle in particular ways, then do a 'catch(Exception e)' as the last catch in case something else slips by.

Re:Could be worse... (1)

rk (6314) | more than 7 years ago | (#15891251)

I'm pretty well-acquainted with the Java exception system. My one Java project was wrapping a C library (converted from FORTRAN!) with a JNI object so that the Java guys could use it in their code.

The running joke after that was that "Randy doesn't know how to program in Java, but he could probably write a JVM if we needed it." :-)

Java's exception system is fairly similar to Python's, which is a language I'm fluent in. I'd give you an example, but /. makes posting whitespace sensitive Python an exercise in frustration.

Re:Could be worse... (1)

elmegil (12001) | more than 7 years ago | (#15892822)

whitespace sensitive Python an exercise in frustration

That's something of a tautology if you ask me ;-). Course, I'm a perl head.

Re:Could be worse... (1)

rk (6314) | more than 7 years ago | (#15893420)

I was a perl head 10 years ago... You don't know the power of the Dark Side. :-)

i have (1)

blinder (153117) | more than 7 years ago | (#15891013)

an example of *exactly* this and the exception, for good reason is swallowed. a batch job that parses an excel spreadsheet. if the producer of said spreadsheet did not provide data for a cell, we don't want to stop doing things or waste time/resources reporting it. just move along and forget it ever happened.

building a fault-tolerant system while saving resources is, in many cases, good programming. especially in this case when the job itself consumes *huge* amounts of resources and can often take 20+ hours to run. every place you can make things faster and lighter, the better... that means swallowing a useless (in this case) array out of bounds exception.

Re:i have (1)

FortKnox (169099) | more than 7 years ago | (#15891120)

But this is a case of writing options for a select dropdown in a JSP (ancient servlet-jsp app). There is no excuse of it and I'm embarassed that this is in a piece of production code I have to maintain.

And I'm actually afraid to take it out ;)

Re:i have (1)

Short Circuit (52384) | more than 7 years ago | (#15891476)

Not being fluent in every language I program in, I at least take the time to note why I do things that seem odd.

I guess the guy didn't leave behind any comments?

Re:i have (1)

ncc74656 (45571) | more than 7 years ago | (#15892489)

I guess the guy didn't leave behind any comments?

He must've been a Real Programmer [funnies.paco.to]. "If it was hard to write, it should be hard to understand and harder to modify." :-)

Re:i have (1)

Cyberdyne (104305) | more than 7 years ago | (#15893668)

But this is a case of writing options for a select dropdown in a JSP (ancient servlet-jsp app). There is no excuse of it and I'm embarassed that this is in a piece of production code I have to maintain.

When I first saw it, I thought it was an optimization in old Java code (remember when it used to be much quicker to run off the end of the array and catch the exception, rather than use a for loop?)... Is there a concurrency problem, so the array could shrink while this method runs? If the original coder hit that exception at some point, it might explain that...

And I'm actually afraid to take it out ;)

Well, it probably doesn't do any harm just sitting there... how about just sticking in a printbacktrace call, so if it ever triggers you'll get it logged and can investigate further?

Re:i have (1)

RevMike (632002) | more than 7 years ago | (#15896523)

Is there a concurrency problem, so the array could shrink while this method runs?

I'm trying to think that one through, and it doesn't make much sense. Arrays are not dynamically resizable, they're fixed in size once created.

I suppose someone could replace the array with a different one inside the loop that was accessing the array. The length attribute of the array would still be available, however, and so there would still be no reason that a reasonable program should generate this exception.

Am I missing anything?

Re:i have (1)

Cyberdyne (104305) | more than 7 years ago | (#15897463)

I'm trying to think that one through, and it doesn't make much sense. Arrays are not dynamically resizable, they're fixed in size once created.

Yep - if it's a true array (not a Vector or similar), you'd be replacing it with one of another size. Being a C programmer at heart, that's the only resizing there is for arrays :-) (You can actually get similar exceptions out of the newer container classes, if you modify them in non-threadsafe ways - not the exception listed in the JE, though, IIRC.)

I suppose someone could replace the array with a different one inside the loop that was accessing the array. The length attribute of the array would still be available, however, and so there would still be no reason that a reasonable program should generate this exception.

Am I missing anything?

Just because the length attribute is available doesn't force you to use it: on early JVMs at least, it was much faster to run off the end of the array and catch the exception rather than use a for loop. As a result, any program developed that way will generate (and catch) that exception every single time it iterates over the array - quite deliberately.

As for the race condition, imagine the array being replaced (with a shorter one) in between you checking the .length field and retrieving a value: without some sort of locking, you have no guarantee that the array whose length you checked matches the array you're operating on later. That's the concurrency problem I mentioned: with other threads around, the modifications aren't necessarily occurring inside your loop - they can happen at any point, including in between you checking the array size and trying to use that information.

Re:i have (1)

RevMike (632002) | more than 7 years ago | (#15896537)

Still, the length of the array should be available. Are you arguing that a 'for' loop is fundamentally less efficient than an infinite loop and exception handler when the data sets are huge? Sam, if you are watching, this is one of those places where any good coder should put a comment.

A Friend, Posted AC For Protection (0)

Anonymous Coward | more than 7 years ago | (#15891138)

Oh, you want to see some bad Java?

One of our apps was rewritten in Java after a long life as a VB app, passing from VB3 to VB6 without a single refactoring along the way. The result was a monument to bad coding practices.

This was one of our first Java front-ends, so we were still deciding how to manage the packages, what to share, what to keep private, etc. Somebody misinterpreted something in a meeting, and came to the erroneous conclusion that, to keep the app-specific UI classes private, they should be implemented as inner classes in the main form.

To compound the error, somebody came to me one day (as I was one of the few people who could tolerate looking at the old VB without going mad), and asked where said old VB code was in the source repository. I fucked up and told him. As a result, much of that UI code ended up a straight translation, VB spaghetti for Swing linguini, in three of the largest anonymous inner classes I've ever seen. I was having flashbacks to pre-MFC WndProcs From Hell. Oh, and did I mention that these anon inners were in the inners in the main form?

There was an entire package of 12-15 classes, some quite complex, stuffed inside that one main form class.

Needless to say, that mess got refactored right quick. It was, fortunately, easy to extract the inner classes. But the pastacode still lives on, as we haven't had time in the schedule to fix it, and, ugly as it is behind the scenes, it's working for the end users.

The Daily WTF (1)

Alioth (221270) | more than 7 years ago | (#15895377)

You know, judging by how much malformed HTML and broken quotes ends up coming up in the Daily WTF forum, I think The Daily WTF should be a subject of the Daily WTF.
Check for New Comments
Slashdot Account

Need an Account?

Forgot your password?

Don't worry, we never post anything without your permission.

Submission Text Formatting Tips

We support a small subset of HTML, namely these tags:

  • b
  • i
  • p
  • br
  • a
  • ol
  • ul
  • li
  • dl
  • dt
  • dd
  • em
  • strong
  • tt
  • blockquote
  • div
  • quote
  • ecode

"ecode" can be used for code snippets, for example:

<ecode>    while(1) { do_something(); } </ecode>
Sign up for Slashdot Newsletters
Create a Slashdot Account

Loading...