0

I have a code like this.

try {
        st = Session.getDbConnection().createStatement();
        delSt = Session.getDbConnection().createStatement();
        rs = st.executeQuery("SELECT msg_id,message,mobile_no FROM sms");

        while (rs.next()) {   
            delSt.executeUpdate("DELETE FROM sms WHERE msg_id = '" + rs.getString(1) + "'");

            System.out.println("Message sent");
        }
        Session.getDbConnection().commit();


    } catch (Exception ex) {
        if (ex.getMessage().startsWith("error occurred at recursive")){
        }
        else{
            logger.error(ex.getMessage(), ex);
        }
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (delSt != null) {
                delSt.close();
            }

        } catch (Exception ex1) {
            logger.error(ex1.getMessage(), ex1);
            ex1.printStackTrace();
        }
        try {
            if (!Session.getDbConnection().isClosed()) {
                Session.getDbConnection().close();
            }
        } catch (Exception ex1) {
            logger.error("ERROR:Closing Database Connection:" + ex.getMessage(), ex);
        }

    } finally {
    }

Now In this case I want to ignore/avoid the exception "error occurred at recursive SQL level 1" for log. But I want to log other than this exception. What's wrong with my code. Thanks

4
  • 2
    avoiding is different than ignoring! Commented Mar 8, 2013 at 9:54
  • 1
    I don't know. What is wrong ? Exceptions etc.? Commented Mar 8, 2013 at 9:54
  • Now that's an ugly example of using if. Instead of if (condition) {} else { doSomething(); } do if (!condition) { doSomething(); } just for the sake of readability. But that's just a sidenote. Commented Mar 8, 2013 at 10:01
  • isn't working means, it's still keeping the exception log "error occurred at recursive" Commented Mar 11, 2013 at 5:59

1 Answer 1

2

I don't think

 if (ex.getMessage().startsWith("error occurred at recursive")){
 }

is particularly safe. You're assuming the exception message is never null (!) and then performing a comparison on the exception message. I would rather catch specific classes of exception e.g.

   catch (SQLException e)

and filter on the class itself. You can also make use of the error codes returned by the database. These are database-vendor specific, note. See SQLException doc for more details and in particular the getErrorCode() method (I'm assuming you're actually catching SQLExceptions here, but the principle is wider)

Other comments. You should be closing the resultset/statements/connection outside the exception handling and in your finally block. You need to free those resources regardless of exceptions/success. Perhaps check out DBUtils.closeQuietly() to do this safely and concisely.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.