| « Prev | Page 1 | Page 2 | Next » |
|
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) |
|
Hey, you're ignoring my comment.
|
|
throw new WTFException("fist");
|
Re: An Interesting Way of Excepting
2007-10-29 09:13
•
by
Mikoangelo
(unregistered)
|
|
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.
|
|
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.
|
|
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."); |
|
This is why I come here.
Fantastic! |
Re: An Interesting Way of Excepting
2007-10-29 09:26
•
by
Eduardo Habkost
(unregistered)
|
"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)" |
|
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. |
Re: An Interesting Way of Excepting
2007-10-29 09:38
•
by
It's a Feature
|
"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. |
Re: An Interesting Way of Excepting
2007-10-29 09:39
•
by
Ian
(unregistered)
|
He does something that will throw an exception so that he can catch it, and then ignore it and throw a new one. Why go to all that trouble when you could just throw the client exception and be done with it? |
|
Test code. Made it into the system. Sloppy process. Not a WTF.
|
Re: An Interesting Way of Excepting
2007-10-29 09:42
•
by
Me
(unregistered)
|
|
The WTFs are:
- He deliberately causes an error to be thrown, but then squashes it, achieving nothing (except a check that the JVM isn't completely insane) - THe Strings are created but never used, adn are simply generated from other String variables. -that the method exists at all 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: throw new ClientException(0, "hi"); |
Re: An Interesting Way of Excepting
2007-10-29 09:45
•
by
sobani
|
The WTF, explained: First, we 'create' a NullPointerException int i = 8/0; After we catch that NullPointerException, we make two different error messages String shortMsg = "short_msg_text1\n" Lastly, we construct a new exception (the proper way) with the error message "hi" throw new ClientException(0, "hi"); Basically all that this method needed was the line throw new ClientException(0, "hi"); |
Re: An Interesting Way of Excepting
2007-10-29 09:45
•
by
It's a Feature
|
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. |
Re: An Interesting Way of Excepting
2007-10-29 09:49
•
by
lungimaster
(unregistered)
|
which in itself is unhelpful WTF |
|
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... |
Re: An Interesting Way of Excepting
2007-10-29 09:52
•
by
Galbrezu
|
ArithmeticException not NullPointerException, but the rest is right. |
Re: An Interesting Way of Excepting
2007-10-29 09:56
•
by
MurfWTF
|
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... |
Re: An Interesting Way of Excepting
2007-10-29 09:56
•
by
jack moxley
(unregistered)
|
|
A:
//throws a client exception void throwClientException() throws Exception { try { 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. |
Re: An Interesting Way of Excepting
2007-10-29 09:58
•
by
Beau "Porpus" Wilkinson
(unregistered)
|
What the hell is it testing? |
Re: An Interesting Way of Excepting
2007-10-29 10:00
•
by
Galbrezu
|
Uhhh... What? I was referring to the fact that int i=8/0 would throw an ArithmeticException not a NullPointerException. |
Re: An Interesting Way of Excepting
2007-10-29 10:04
•
by
Andrew Trumper
(unregistered)
|
|
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. |
Re: An Interesting Way of Excepting
2007-10-29 10:05
•
by
Shinobu
(unregistered)
|
And if the application just shuts down with a "hi!" you wouldn't know that something went wrong? |
Re: An Interesting Way of Excepting
2007-10-29 10:05
•
by
Medinoc
(unregistered)
|
And he was refering to your "the rest is right". |
Re: An Interesting Way of Excepting
2007-10-29 10:07
•
by
MurfWTF
|
|
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. |
Re: An Interesting Way of Excepting
2007-10-29 10:19
•
by
ddalex
(unregistered)
|
|
At least a Division by Zero not Null Pointer :)
|
|
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; |
Re: An Interesting Way of Excepting
2007-10-29 10:28
•
by
coward
(unregistered)
|
|
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()? |
Re: An Interesting Way of Excepting
2007-10-29 10:29
•
by
Martin
(unregistered)
|
|
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 |
Re: An Interesting Way of Excepting
2007-10-29 10:30
•
by
DaveK
|
Feel free to cry like a baby about it. |
|
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? |
Re: An Interesting Way of Excepting
2007-10-29 10:33
•
by
Stupidumb
(unregistered)
|
|
Awesome
|
Re: An Interesting Way of Excepting
2007-10-29 10:48
•
by
Bob Kaufman
(unregistered)
|
|
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.
|
Re: An Interesting Way of Excepting
2007-10-29 10:55
•
by
ruijoel
|
|
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. |
Re: An Interesting Way of Excepting
2007-10-29 11:04
•
by
sobani
|
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" |
Re: An Interesting Way of Excepting
2007-10-29 11:06
•
by
java.lang.PedanticUserException
(unregistered)
|
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) |
Re: An Interesting Way of Excepting
2007-10-29 11:13
•
by
M Cassidy
(unregistered)
|
|
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? |
|
Chuck Norris can divide by zero.
|
|
I know I don't like it when my clients take exception to greet me.
|
|
Snippet from a sandbox component of JSF.
com.sun.faces.sandbox.util.UrlMatcher:
|
Re: An Interesting Way of Excepting
2007-10-29 11:37
•
by
MurfWTF
|
It's a good thing you don't think that, as it's wrong.
Sorry to be pedantic again, but it doesn't construct an error message from multiple strings. An error message is assigned from one static string. I think this matters because assigning a variable to a static value and letting it drop out of scope is pretty minor. Unnecessary string concatenation is not (I've seen it bring servers down). 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. |
Re: An Interesting Way of Excepting
2007-10-29 11:40
•
by
ammoQ
|
The real WTF is the drastic increase of carbondioxide emissions due to the increased network traffic and file size for all those meaninless spaces caused by Sun's Java code conventions. Sun obviously has a hidden agenda to get rid of all of us. Responsible programmers never use meaningless whitespace, long variable names or comments in their code. |
|
Notes below:
String shortMsg = "short_msg_text1\n" I'm not sure how Java handles concatenation and quoted strings, but in C# the result of this would be literally: shortMsg = "short_msg_text1 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). |
Re: An Interesting Way of Excepting
2007-10-29 11:41
•
by
Daz
(unregistered)
|
Shouldn't it say 'Bye!'??? Daz |
Re: An Interesting Way of Excepting
2007-10-29 12:01
•
by
RobertB
|
Then the programmer must be James Anderson, right? He's the guy who "invented" nullity, the value of zero divided by zero, and is trying to "market his ideas for transreal arithmetic to investors." |
|
I porpose a new name for this article:
A throws by any other name Thank you |
Re: An Interesting Way of Excepting
2007-10-29 12:17
•
by
nobody
(unregistered)
|
Shouldn't it really say "Bye!" |
Re: An Interesting Way of Excepting
2007-10-29 12:18
•
by
Kaitnieks
(unregistered)
|
|
O, hai 2 u 2, lol!
|
| « Prev | Page 1 | Page 2 | Next » |