Become a fan of Slashdot on Facebook


Forgot your password?
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 internet speed test! ×
User Journal

Journal FortKnox's Journal: "throws Exception" is not acceptable!! 27

Ugh, I hate maintaining old code.

Your method should rarely (JUnit tests, and other rare instances) 'throws Exception.'

Its just plain gross. Especially because you were a lazy ass when the only exception BEING thrown is an SQLException!

This code I'm maintaining is from an overseas job.

The only thing I can tell my boss is:
You can't polish a turd

And before ya bitch, I spend all yesterday out with my son riding his bike, then afterward going out to get him ice cream, then up all night with him cause he had leg cramps (must rode too long), so I'll get to the photos late tonight (hopefully... I got training class until at least 9).
This discussion has been archived. No new comments can be posted.

"throws Exception" is not acceptable!!

Comments Filter:
  • If the method is supposed to return a value do you want it to return null or some "empty value" if it fails or actually cause an Exception because you're in an exceptional state?
    • "thows Exception" is like saying "thows something" - if your routine throws Foo, Bar, and Baz you should declare it as throwing Foo, Bar, and Baz, so that the caller knows what exceptions to expect (and thus what to catch).

      It's about like writing a routine as

      int foo(...)

      rather than

      int foo(int soldiers, enum Color color)

    • Throws a generic Exception is not an exceptionl case, because the error, java.lang.Exception, is a generic exception type. If you're in a special case, you'll probably want to trap that with its own type. Throwing Exception just says, "Something happened. I'm not telling you what happened. So go fix it." You're better off throwing an Error in those cases.

    • wowbagger kinda explained it. I'm 100% for Exception throwing, but in java, "java.lang.Exception" is the parent of all exceptions. So if I'm throwing "SQLException" and "NumberFormatError", then both of these method declarations are compilable:
      void foobar() throws SQLException, NumberFormatException {...}
      void foobar() throws Exception {...}

      The second one works because of polymorphism, but the first one is still a better way of doing it. The reason is when I call the method:
      Now if I used the
  • If it were not for turds found in poor code, how else would root kits steal all of our personal information? I remember back when I thought I was the shizzle for tossing around a little VBScript.... Oh how times have changed... I learned C, C++, PERL, Java, .NET, and now I've been assigned to recode a bunch of small crappy apps that were written by an idiot in VBScript... yeah, you guessed it... my former VBS apps.

    I feel your pain, yet it is that very pain that allows me to feed my younguns.
  • by btlzu2 ( 99039 ) *
    I thought you were only coding in Satan's language now. What are you doing with an acceptable, nice language? :)
    • I thought I splained it before. I'm in the .NET group, but still do my java work. I'll learn C# and expand my knowledge but most of the jobs I'll be doing is in Java.

      The first thing they want me doing is to make a java API available in C#. So I'm like the "Java Guy" in the .NET group...
  • My current gig is "upgrading" a poorly-written app to use, um, an MVC framework instead of 200 .jsp files.

    Oh, did I mention that one of the first lines of every file is "try" and one of the last lines is "catch (Exception e)"?

    I actually got to talk to the "programmer" (he sure weren't no 'developer') about what his "thinking" was behind doing this. The answer: so when the program crashes, the user won't see it. Never mind that there is no easy way to diagnose problems. Suppose the code does three SQL fun

    • I've done that, but ONLY for R&D work; i.e. never goes to code inspection just has to friggin' run NOW.

      And when I go back to it, (IF anyone ever goes back to it) it gets taken out.

      However I always log log log log logl og lgo gllo gool my exceptions because they are my lifes blood.

      The WORST- was when using some package to convert a stream into xml and it dying on some subroutine called "convert character"- what character?! WTF?! My xml objects are 20K on 1 line!!! (it breaks vi)
      • That shipping program that I wrote actually emails me everytime it throws an exception. Why? because the shipping guys when it gave them an error that said, "X has happened, please notify IT to have this problem diagnosed before closing the window." I'd get a call, "Hey! My computer is down."
        "Down? You can't log on?"
        "I can log on, I just can't do nothun'."
        "No email or internet."
        "No, them's working fine."
        "Ok, so tell me what ISN'T working."
        "My shipping program threw up a buncha gobbledygook on
        • TL TL TL.....

          You should be catching the exception in your highest level view class (like your actions in struts or servlets if you are doing the old-skool thang), and reporting something nice to the user and logging the actual error and stacktrace.

          That way the user knows something went wrong and (possibly) how to avoid it, and you get the exact problem so you can (possibly) fix it so it won't happen again...
          • That's how I'm doing it now. It's catches each error type that could possibly exist and reports a custom error to the user for each one, along with what they need to do to correct it. But instead of logging it, I'm sending emails to myself and materials (along with the FedEx tracking number) so they know whether to cancel that shipment or mark it as shipped, depending on the error. And yes, we're still using servets and JSPs.
  • I have just been thrust into a position at my company where some java is a requirement. Since he left so suddenly, and I have proven myself with a number of other projects using php, and I also am the only one that is even slightly familiar with the systems he was working on they figured what the heck? That he didn't document ONE single thing coupled with the fact that I am the only other one who has ever worked on these systems made me a shoe-in I guess. One problem, I don't know Java. At all. Nope no
    • I meant to hit the "plain old text" option.

      Here it is again:

      I have just been thrust into a position at my company where some java is a requirement. Since he left so suddenly, and I have proven myself with a number of other projects using php, and I also am the only one that is even slightly familiar with the systems he was working on they figured what the heck?

      That he didn't document ONE single thing coupled with the fact that I am the only other one who has ever worked on these systems made me a shoe-i
      • "Thinking in Java" by Bruce Eckels. Its available for free on the net. Read it, live it, love it. Its how I learned Java.
      • Download the Eclipse IDE. Download the proper (like 1.4.2) Java SDK. Dot completion is your biggest friend and Ctrl+Space will recommend things you can do at any point. Ctrl+Click will take you to the declaration of what you're clicking. Let Eclipse compile and do pretty much everything for you. Keep the API documentation, found here [], handy. Look for tutorials for specifics on Sun's site. Grab a book, like an O'Reily, and skim it. Look at existing code that works and is clearly written.

        That's the fastest wa

  • You've got no one to blame but yourself. If you had finished unread when you started, the code would no longer be 'old'.

    Is Joey on a two-wheeler? Training wheels? If they're off, I'd love to know how you did it.
    • He's on a little red-rider metal tricycle. No two wheeler... not even close. He just started learning how to petal hard to go fast...
      • That's what I figured, as Joe was about that stage 12 mos. ago. But I was hoping:)

        • Kiddo finally started pedaling her tricycle on September 11, 2004. She was just a couple months shy of 4 years old. Gross motor skills are not her strong suit. Neither is speed.

          I don't think it's going to take Squirt that long -- she's already eyeballing that tricycle and pulling up to fondle the handlebar streamers. :-)

  • I'm from the Yoda school of coding:
    Do, or do not. There is no try.
    Before exceptions, there was the setjmp() and longjmp() - messy, so there was an incentive to keep everything clean.

    Now, everyone says "what the hell, I'll just write it and wrap it in a try/thows exception" instead.

  • I just ran into the analog problem

    try{ .... lots of code ....}
    catch( Exception e )
    { System.out.println("Socket can't connect"); }

    Now while there is lots of socket code, in there, it isn't the only exception throwing code, and because the exception catcher just generalizes all exceptions away you spend a couple hours wondering why you are getting a socket error when the code you added had nothing to do with sockets.

    ugh, I just simply hate having to pick up other people's code. Especially code that has 0
  • Can't you freeze the turd and make an ice sculpture?
  • bananas, calcium (banana milkshake?), and a lot of water

    light stretching

    problems be gone! :-)
    • I just learned about the banana thing. Joey loves bananas, so I'll have to get him eating those daily again... And he's a good milk drinker (at least a (kids) cup a meal), so that isn't an issue.... Thanks for the advice :-D

The means-and-ends moralists, or non-doers, always end up on their ends without any means. -- Saul Alinsky