- 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
-1st!
Admin
TRWTF is that this article doesn't have next or previous links!
Admin
Because they were labouring under a coding standard that mandated some form of idiocy, and this form was the most palatable?
Admin
They're almost certainly doing this to get the call stack logged. It is a WTF, but only a fairly minor one caused by ignorance of Thread.getStackTrace - or maybe this code is older than Java 1.5 (you didn't always used to be able to avoid using an exception to get it iirc).
Hopefully as well as submitting this here, the OP also made sure the author of the code was informed about the better way.
Admin
Even if they were limited to Java 1.4, there was no need to actually throw the exception.
log.debug(...., new Exception())
would have done just fine.Admin
The reason why abusing exceptions is bad is because creating the stacktrace is really expensive. If you've already made the exception, the actual throwing doesn't really matter.
If you're going to accept the cost of a stacktrace, a "throw new Exception()" wouldn't have made me cringe too hard.
Also, I had to look up what doubleValue() actually does - it's an explicit cast-to-double operation, for whatever reason. Nothing to do with doubling a value.
Admin
Most logging frameworks in Java have a log(String message, Throwable cause) method. I'm not aware of one with a log(String message, StackTraceElement[] causeStack). I've got no explanation of the actually throwing and catching of that exception, though.
Admin
TRRWTF is that now it does, a year and change after the fact!
Admin
In brief, there's an interface called
java.lang.Number
, which provides anxValue
method for each of the six number primitives. It's implemented by the boxed primitives (Integer
,Double
, and the like), as well as the atomic and big number classes. It's not that useful, but that's the reason.Ordinarily you'd just use typical casting notation -
(double) i
- but there's nothing "ordinary" about this code, anyway.Admin
Ironically, this is one of the specific cases where the HotSpot JVM can choose to omit the stack trace from the NullPointerException for optimization purposes:
https://stackoverflow.com/questions/58696093/when-does-jvm-start-to-omit-stack-traces
Admin
Very early in my coding career I wanted to be able to generate a stack trace of all threads states on demand (w/o using gdb) to diagnose a hangup in the field. So I made a special remote API call that would force a NULL ptr dereference. The OS (early release of OSX circa y2k) would generate a nice little crash log for me with all the data I wanted. I cringe at that now. (After figuring out the problem the API was removed)
Admin
i seem to remember an old story about a program that worked fine in Debug mode but not in normal mode...i forget the details.
Admin
So ... for once, doing it completely the wrong way results in completely the wrong output?.
I find this strangely comforting.
Admin
Once again the WTF can be explained away if the age of the code is known - seems a lot is complaining about code that was written before a feature existed.
Admin
See Maia Everett's comment. A WTF is a WTF.
Admin
I've seen similar code with ((String)null).toString() to get a NullPointerException with stack trace to pass to the logging method. Replaced that one with new Throwable("For stack trace"). It made both the code and the logs much clearer; It was Java 1.1.
Admin
Yeah. Even if what doubtingposter says is accurate and the bulk of the cost of an exception is creating the exception in the first place. Without throwing the exception, the intent of the code is more clear. And could be more easily and confidently replaced with something that will just log the stack trace later.
Admin
Or even just create a new NullPointerException() and then throw it. No need to actually dereference a null variable.
Admin
If the code does what the names of the variables and functions suggest, it is only used to trace execution times per stack depth and initial instruction.
Never mind the (lack of) elegance of the actual code; why would anybody be interested in the raw data it produces?
This truly makes it an exceptional mystery, which is further substantiated by the fact that it first appeared on CodeSOD on 2007-04-04: back then nobody could come up with a "that-must-be-it"-explanation too. Perhaps it's time to finally settle on the conclusion that "it just lacks sense" and move on.
Admin
I'd be surprised if 'creating the exception' was slow, as I'd think at that point it is just another POJO.
One might possibly create an Exception object and throw it from numerous places in the code, for which one could expect the stacktrace to indicate the line it was thrown from, not the line it was created/instantiated on.
I would expect and assume (and maybe make an ass) that the stacktrace is only generated as part of the throw and not the new
Then again, I wouldn't do this so my assumptions may well be completely off the mark.
Admin
My favorite:
*0();
In the old days, Sun would segfault. Dec would reboot.