- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
short_cmt_1
Admin
Yet another of those really informative error messages - "Something went wrong", "Unexpected error occurred", "hi!", "OMGWTFBBQ?", "F-word!" and so on. WTF? Yes. Something new? Hardly. (And yes, I realize this is only one of the things that are wrong with that snippet)
Admin
Hey, you're ignoring my comment.
Admin
throw new WTFException("fist");
Admin
Or, perhaps, it could be related to the fact that he generates a division by zero error, catches it, ignores it, and then generates the exception the function was supposed to do. I dunno.
Admin
This would be partially excusable if the guy just didn't know about the throw statement. But there it is, in the catch(Exception) block code goes to after an explicit divide by zero error. This does not make me smile. At all.
Admin
I just love the "hi". But I've done things like this in testing - so all we can say here is that someone really should have read their code more carefully before going to production.
throw new Exception("You suck.");
Admin
This is why I come here. Fantastic!
Admin
"Just got this gadget. It does some really interesting things. Look."
"Yet another of those molecules, composed by atoms. Matter? Yes. Something new? Hardly. (And yes, I realize this is only one of the molecules that compose this object)"
Admin
Would someone be so kind as to explain the WTF?
To me, this looks like some test code that checks how the clientException is thrown. If the WTF is supposed to be the sloppy documentation or the throw then catch, then I don't think this is a very good one.
Admin
"Something went wrong" definitely would have been a more useful error than "hi!"--which is what this code will do. If the application suddenly up and went "hi!" to me, then shut down, I'd definitely be wondering WTF!? At least with "Something went wrong", I'd know that something went wrong.
Admin
Admin
Test code. Made it into the system. Sloppy process. Not a WTF.
Admin
The WTFs are:
Otehr bad things are that: -He doesn't specify what exception is thrown in the method signature -the message is completely useless.
The whole method could be re-written as:
Admin
The WTF, explained:
First, we 'create' a NullPointerException
After we catch that NullPointerException, we make two different error messages
Lastly, we construct a new exception (the proper way) with the error message "hi"
Basically all that this method needed was the line
Admin
It is a WTF if you, as the programmer trying to debug this application, stumble onto this production code, and wonder, "does anything actually use this?"
What would make it NOT a WTF: comments that explained its purpose and the fact that it should never be used in production. Since there are no comments, and since the function of this code is less than absolute worthlessness (since if it were used it would produce an error message that would hide the fact that there is an error), the best course of action would be to remove the code outright, and correct any code that happened to call it.
Admin
which in itself is unhelpful WTF
Admin
This looks to me like test code that's slipped into production, I'd guess as a way of generating exceptions from the client to make sure the server can cope. Questionable, but not really a WTF.
This does remind me of my favourite WTF though - return new ClassCastException(). Anyone remember that? Long since lost in the archives sadly...
Admin
ArithmeticException not NullPointerException, but the rest is right.
Admin
Well, not entirely. The error messages aren't 'generated', the compiler would optimize those strings out to one static string, so no string concatenation would happen in that method, just assigning a variable from a static one.
Pedantic I know, but there you go...
Admin
A: //throws a client exception void throwClientException() throws Exception {
B: int i = 8/0; } catch(Exception e) { C: String shortMsg = "short_msg_text1\n" + "short_msg_text2"; String detailMsg = "detail_msg_text1\n" + "detail_msg_text2\n" + "detail_msg_text3"; D: throw new ClientException(0, "hi"); }
}
Ive subdivided it into 3 parts so I can explain whats wrong.
A: You can declare this as throws ClientException (not a biggie, but their is no need for the poor comment given).
B: The only test going on here is that for 8/0 throwing an exception which is obvious. Why generate an exception if your going to throw one anyway. It is a convoluted way of saying "if(true) throw new Exception();" At least the later would be more efficient.
C: You are constructing 2 strings which aren't being used anywhere.
D: Your throwing an exception in the catch block when you could simply throw it at the beginning of the method. And its not particularly descriptive or helpful for debugging.
This method is completly unnecessary it would make more sense to actually throw this exception rather than calling the method, otherwise the stack trace will trace to this method rather than to the point where the method was called. Making debugging a little more complicated. It is also generally not great practice to throw exceptions from within catch blocks, if you do you should chain it and include the exception in the constructor of the exception your throwing, this isn't happening.
This is probably the most inefficient use of exceptions I have ever seen.
Admin
What the hell is it testing?
Admin
Uhhh... What?
I was referring to the fact that int i=8/0 would throw an ArithmeticException not a NullPointerException.
Admin
Here's my list:
Method has "//throws a client exception"
when it should simply throw a ClientException. This would have made the comment unnecessary and allowed clients of the function to catch ClientException instead of having to catch (Exception ..) and manually figuring out which exception was thrown.
Manually causing a java.lang.ArithmeticException: / by zero for absolutely no reason.
building up some sort of place holder error messages then ignoring them.
unconditionally throwing a ClientException... with message "hi"
There's also secondary questions as to how code this unspeakably bad got into the code base in the first place.
Admin
Admin
Admin
I was referring to the "but the rest is right" comment, which isn't true. I should've quoted the original post but I hate large quotes.
Simply put, no strings are constructed or concatenated in that method, just assigned.
Admin
At least a Division by Zero not Null Pointer :)
Admin
The real WTF is obviously the use of the magic number 8. Should be a constant, i.e.
static final int EIGHT=8; ... int i=EIGHT/0;
Admin
Yeah, the REAL WTF here is lack of comments!
Writing a method whose internals (one you remove all of the useless stuff) can be reduced to a simple one-liner is truly awesome good code.
I mean why call throw new Exception() when you can call a method that calls throw new Exception()?
Admin
Perhaps this method only should throw an Exception as long as division by zero is not supported. Perhaps in some future JVMs this will be supported and the developer already thought of that. Some using code could be
Captcha: doom
Admin
Feel free to cry like a baby about it.
Admin
My guess is that this programmer wanted to throw an exception, but did not know how. Then when searching for code that does throw an exeception (wanting to copy and paste it), only finds throws from catch blocks, and then assumes that is the required context for a throw. That is when he comes up with the divide by zero because he wants to make sure the catch is activated. Plausible enough?
Admin
Awesome
Admin
Looks like a junior programmer was learning the mechanics of writing and using an Exception... however in an entirely inappropriate place. Hello.java would have been a more prudent place to try this.
Admin
In addition, the fact that it declares 'throws Exception', makes all calling methods have to declare 'throws Exception' or catch that Exception, which maybe they don't. So, it's just improper use of exceptions (I reckon it's very common in Java, anyway). The short message and the detail message, even if they were actually used, have no meaning whatsoever.
the text is: short_msg_text1 short_msg_text2
which isn't very informative.
Or maybe, just maybe, this was a code snippet which someone put there to show how to properly throw an exception. Except he was completely clueless.
Oh, and thanks to the marvels of Javadoc, the comment could be replaced by /** @throws ClientException always*/ and become more readable
I second that. Awesome.
Admin
I know that, but if I read the code, I don't think: "Hey, java will ignore that unused String!", but instead: "... and here we construct an error message from multiple strings"
Admin
The real WTF is your blatant disregard for the Sun Java code conventions, you missed the spaces around the operators:
int i = EIGHT / 0;
Don't mess with professional java programmers ;)
Nice article though, made me LOL at the job, there are some many things wrong with that code, i could probably type a full page essay on why the programmer who wrote it should be fired (or a more kind alternative, be given a SCJP study guide, although kind is relative here)
Admin
Is it just me, or is the general popularity of this site beginning to attract the very crowd we're basically all on about.
And do they think they aren't part of that crowd?
Admin
Chuck Norris can divide by zero.
Admin
I know I don't like it when my clients take exception to greet me.
Admin
Snippet from a sandbox component of JSF.
com.sun.faces.sandbox.util.UrlMatcher:
Admin
The concatenation is done at compile time and the static String is constructed when the class is loaded. This method just assigns the value of a variable to a static string.
Admin
Admin
Notes below:
I'm not sure how Java handles concatenation and quoted strings, but in C# the result of this would be literally:
If short_* and detail_* are variables, they would never actually get interpreted -- you would just have 2 strings with those exact values. This also raises the question of scope. If the short_* and detail_* are indeed variables, where are they set? We don't see them being set inside this method. Maybe this can be attributed to my lack of knowledge in regards to Java. I know some languages (like PHP) will interpret variables within quoted strings, but then again, PHP's variables are easily distinguishable by the dollar sign (e.g. $short_msg_text1).
Admin
Shouldn't it say 'Bye!'???
Daz
Admin
Admin
I porpose a new name for this article:
A throws by any other name
Thank you
Admin
Shouldn't it really say "Bye!"
Admin
O, hai 2 u 2, lol!