SQLException ftw



  • I recently encountered this Java code snippet:

    [code]public class DummySubclass extends DummyClass {

    @Override
    public void doSomething(String someParameter) throws SQLException {
    //snip -- there was code doing something important here

    if (false) {
    throw new SQLException("never occurs");
    }
    }
    }[/code]
    (class and method names anonymized to protect the guilty)

    I wondered about the purpose of that if condition and the exception never being thrown. The only thing I could think of was that maybe that developer thought he had to comply to the overriden method fully and completely, and that might have been declared as throwing a SQLException. And the compiler seems to have complained that he declared throwing an exception but actually never did. So he had to come up with this great bit of code to trick the compiler.

    So I took the next step and verified my assumption. The superclass looked like this:

    [code]public void doSomething(String someParameter) throws SQLException {
    if (someParameter == null) {
    throw new SQLException("Precondition not met");
    }
    //snip -- there was code doing something important here
    //which had nothing to do with database access whatsoever
    }

    [/code]

    And this really takes the cake. The code has absolutely nothing to do with database access. But it throws a SQLException.

    Great times  :-(

    BTW: I'm sorry if the code snippets don't show correctly. I tried hard, but I couldn't master this post editor box. Everything looked fine in the "Compose" box and in the "HTML" window, but the "Preview" box messed it all up.



  • @TheRider said:

    BTW: I'm sorry if the code snippets don't show correctly.

    They don't. Raw editor FTW.

    public class DummySubclass extends DummyClass {
        @Override
        public void doSomething(String someParameter) throws SQLException {
            //snip -- there was code doing something important here
    
        if (false) {
            throw new SQLException("never occurs");
        }
    }
    

    }

    public void doSomething(String someParameter) throws SQLException {
        if (someParameter == null) {
            throw new SQLException("Precondition not met");
        }
    
    //snip -- there was code doing something important here
    //which had nothing to do with database access whatsoever
    

    }

    @TheRider said:

    And the compiler seems to have complained that he declared throwing an exception but actually never did.

    And that would be just silly. The throws clause constrains what you can throw, not what you must.



  • @Spectre said:

    They don't. Raw editor FTW.
    Thanks

    @Spectre said:

    And that would be just silly. The throws clause constrains what you can throw, not what you must.
    I know. You are right, it wasn't the compiler. But it looks like the original developer of that crap was uber-cautious.

     



  • Where I work, we use some Oracle JPublisher generated classes (based on user types in an Oracle DB).

    Every set method in these classes is declared with throws SQLException despite none of them actually accessing the database.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.