• (cs)

    short_cmt_1

  • (cs)

    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)

  • (cs)

    Hey, you're ignoring my comment.

  • Pyro (unregistered)

    throw new WTFException("fist");

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

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

  • (cs)

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

  • (cs)

    This is why I come here. Fantastic!

  • Eduardo Habkost (unregistered) in reply to Sad Bug Killer
    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)"

  • (cs)

    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.

  • (cs) in reply to Sad Bug Killer
    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.

  • Ian (unregistered) in reply to sc0ticus
    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?

  • RK (unregistered)

    Test code. Made it into the system. Sloppy process. Not a WTF.

  • Me (unregistered) in reply to sc0ticus

    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");
  • (cs) in reply to sc0ticus
    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");
  • (cs) in reply to RK
    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.

  • lungimaster (unregistered) in reply to Me
    Me:
    The whole method could be re-written as:
    throw new ClientException(0, "hi");

    which in itself is unhelpful WTF

  • (cs)

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

  • (cs) in reply to sobani
    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.

  • (cs) in reply to Galbrezu
    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...

  • jack moxley (unregistered) in reply to sc0ticus

    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.

  • Beau "Porpus" Wilkinson (unregistered) in reply to RK
    RK:
    Test code. Made it into the system. Sloppy process. Not a WTF.

    What the hell is it testing?

  • (cs) in reply to MurfWTF
    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.

  • Andrew Trumper (unregistered) in reply to sobani

    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.

  • Shinobu (unregistered) in reply to It's a Feature
    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?
  • Medinoc (unregistered) in reply to Galbrezu
    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".

  • (cs) in reply to Galbrezu

    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.

  • ddalex (unregistered) in reply to sobani

    At least a Division by Zero not Null Pointer :)

  • (cs)

    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;

  • coward (unregistered) in reply to sc0ticus

    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()?

  • Martin (unregistered) in reply to jack moxley

    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

  • (cs) in reply to TGV
    TGV:
    Hey, you're ignoring my comment.

    Feel free to cry like a baby about it.

  • 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?

  • Stupidumb (unregistered) in reply to Eduardo Habkost

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

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

  • (cs) in reply to Stupidumb

    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.

  • (cs) in reply to MurfWTF
    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"

  • java.lang.PedanticUserException (unregistered) in reply to ammoQ
    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)

  • M Cassidy (unregistered) in reply to RK

    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?

  • (cs)

    Chuck Norris can divide by zero.

  • (cs)

    I know I don't like it when my clients take exception to greet me.

  • 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;
        }</pre>
    
  • (cs) in reply to sobani
    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.

  • (cs) in reply to java.lang.PedanticUserException
    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.
  • (cs)

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

  • Daz (unregistered) in reply to Shinobu
    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

  • (cs) in reply to Martin
    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."

  • xix (unregistered)

    I porpose a new name for this article:

    A throws by any other name

    Thank you

  • nobody (unregistered) in reply to Arancaytar
    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!"

  • Kaitnieks (unregistered)

    O, hai 2 u 2, lol!

Leave a comment on “An Interesting Way of Excepting”

Log In or post as a guest

Replying to comment #:

« Return to Article