Comment On An Interesting Way of Excepting

While spelunking through a cavernous J2EE application, Matt stumbled across this method, buried in a base Client Object class... [expand full text]
« PrevPage 1 | Page 2Next »

Re: An Interesting Way of Excepting

2007-10-29 09:04 • by TGV
short_cmt_1

Re: An Interesting Way of Excepting

2007-10-29 09:06 • by Sad Bug Killer
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)

Re: An Interesting Way of Excepting

2007-10-29 09:09 • by TGV
Hey, you're ignoring my comment.

Re: An Interesting Way of Excepting

2007-10-29 09:12 • by Pyro (unregistered)
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.

Re: An Interesting Way of Excepting

2007-10-29 09:19 • by 008 (unregistered)
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.

Re: An Interesting Way of Excepting

2007-10-29 09:22 • by Arancaytar
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.");

Re: An Interesting Way of Excepting

2007-10-29 09:23 • by alunharford
This is why I come here.
Fantastic!

Re: An Interesting Way of Excepting

2007-10-29 09:26 • by Eduardo Habkost (unregistered)
158960 in reply to 158951
Sad Bug Killer:
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)


"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)"

Re: An Interesting Way of Excepting

2007-10-29 09:27 • by sc0ticus
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
158963 in reply to 158951
Sad Bug Killer:
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)


"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)
158964 in reply to 158961
sc0ticus:
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.
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?

Re: An Interesting Way of Excepting

2007-10-29 09:39 • by RK (unregistered)
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)
158966 in reply to 158961
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
158967 in reply to 158961
sc0ticus:
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.


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"

+ "short_msg_text2";
String detailMsg = "detail_msg_text1\n"
+ "detail_msg_text2\n"
+ "detail_msg_text3";

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
158968 in reply to 158965
RK:
Test code. Made it into the system. Sloppy process. Not a WTF.


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)
158969 in reply to 158966
Me:

The whole method could be re-written as:
throw new ClientException(0, "hi");


which in itself is unhelpful WTF

Re: An Interesting Way of Excepting

2007-10-29 09:49 • by MurfWTF
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
158972 in reply to 158967
sobani:
sc0ticus:
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.


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"

+ "short_msg_text2";
String detailMsg = "detail_msg_text1\n"
+ "detail_msg_text2\n"
+ "detail_msg_text3";

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");


ArithmeticException not NullPointerException, but the rest is right.

Re: An Interesting Way of Excepting

2007-10-29 09:56 • by MurfWTF
158973 in reply to 158972
Galbrezu:

ArithmeticException not NullPointerException, but the rest is right.


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)
158974 in reply to 158961
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)
158975 in reply to 158965
RK:
Test code. Made it into the system. Sloppy process. Not a WTF.


What the hell is it testing?

Re: An Interesting Way of Excepting

2007-10-29 10:00 • by Galbrezu
158976 in reply to 158973
MurfWTF:
Galbrezu:

ArithmeticException not NullPointerException, but the rest is right.


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...


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)
158977 in reply to 158967
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)
158978 in reply to 158963
It's a Feature:
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.
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)
158979 in reply to 158976
Galbrezu:

Uhhh... What?

I was referring to the fact that int i=8/0 would throw an ArithmeticException not a NullPointerException.

And he was refering to your "the rest is right".

Re: An Interesting Way of Excepting

2007-10-29 10:07 • by MurfWTF
158980 in reply to 158976
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)
158981 in reply to 158967
At least a Division by Zero not Null Pointer :)

Re: An Interesting Way of Excepting

2007-10-29 10:22 • by ammoQ
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)
158983 in reply to 158961
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)
158984 in reply to 158974
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

boolean divByZeroSupported = true;
try {
throwClientException()
} catch(ClientException ce) {
divByZeroSupported = false;
}
...


Captcha: doom

Re: An Interesting Way of Excepting

2007-10-29 10:30 • by DaveK
158985 in reply to 158952
TGV:
Hey, you're ignoring my comment.


Feel free to cry like a baby about it.

Re: An Interesting Way of Excepting

2007-10-29 10:31 • by Jan (unregistered)
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)
158987 in reply to 158960
Awesome

Eduardo Habkost:
Sad Bug Killer:
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)


"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)"

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
158989 in reply to 158987
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

Stupidumb:
Awesome

Eduardo Habkost:
Sad Bug Killer:
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)


"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)"


I second that. Awesome.

Re: An Interesting Way of Excepting

2007-10-29 11:04 • by sobani
158990 in reply to 158980
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.


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)
158991 in reply to 158982
ammoQ:
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;


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)
158992 in reply to 158965
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?

Re: An Interesting Way of Excepting

2007-10-29 11:31 • by T_PAAMAYIM_NEKUDOTAYIM
Chuck Norris can divide by zero.

Re: An Interesting Way of Excepting

2007-10-29 11:32 • by PerdidoPunk
I know I don't like it when my clients take exception to greet me.

Strange Messages have a culture!

2007-10-29 11:36 • by Dube (unregistered)
Snippet from a sandbox component of JSF.

com.sun.faces.sandbox.util.UrlMatcher:

public Map<String, String> getInjections(String url) {
Matcher m = Pattern.compile(urlPattern).matcher(url);
if (m.matches()) {
int numGroups = m.groupCount();
if (numGroups != elSnippets.size()) {
throw new RuntimeException("Aaaaaahhhhh!");
}
Map<String, String> elMap = new HashMap<String, String>();
for (int i=0; i<elSnippets.size();i++) {
elMap.put(elSnippets.get(i), m.group(i+1));
}
System.out.println();
return elMap;
}
return null;
}

Re: An Interesting Way of Excepting

2007-10-29 11:37 • by MurfWTF
158996 in reply to 158990
sobani:

I know that, but if I read the code, I don't think: "Hey, java will ignore that unused String!"

It's a good thing you don't think that, as it's wrong.

sobani:

but instead: "... and here we construct an error message from multiple strings"

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
158997 in reply to 158991
java.lang.PedanticUserException:
ammoQ:
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;


The real WTF is your blatant disregard for the Sun Java code conventions, you missed the spaces around the operators:

int i = EIGHT / 0;


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.

Re: An Interesting Way of Excepting

2007-10-29 11:41 • by swaj
Notes below:
        String shortMsg = "short_msg_text1\n"

+ "short_msg_text2";
String detailMsg = "detail_msg_text1\n"
+ "detail_msg_text2\n"
+ "detail_msg_text3";

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

short_msg_text2"

detailMsg = "detail_msg_text1
detail_msg_text2
detail_msg_text3"

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)
158999 in reply to 158978
Shinobu:
It's a Feature:
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.
And if the application just shuts down with a "hi!" you wouldn't know that something went wrong?


Shouldn't it say 'Bye!'???

Daz

Re: An Interesting Way of Excepting

2007-10-29 12:01 • by RobertB
159003 in reply to 158984
Martin:
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

boolean divByZeroSupported = true;
try {
throwClientException()
} catch(ClientException ce) {
divByZeroSupported = false;
}
...


Captcha: doom

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."

Re: An Interesting Way of Excepting

2007-10-29 12:12 • by xix (unregistered)
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)
159011 in reply to 158957
Arancaytar:
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.");


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!
« PrevPage 1 | Page 2Next »

Add Comment