Results 1 to 8 of 8

Thread: error capturing

  1. #1
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default error capturing

    hello,
    I have a question about trapping errors. I have a method that inserts data into a database. One of my fields requires a unique entry, so when my program enters a string that already exists, i get a general SQL error. Of course, I know what that means but how do i trap that error, and distinguish it from other SQL type errors so that I can provide the appropriate warning to the end user?

    Right now i'm just using try/catch like this:

    Java Code:
     try {
                 //do something here
                } catch (Exception e) { 
                    callingFrame.setColor(Color.red);           
                    callingFrame.setLable("Error adding district:" + e.toString());
                    e.printStackTrace();
                } finally {
                   
                }
    As i mentioned, this catches the error but how can i say "if the error is an access database error warning that you already have this record, do this" if its this error, do that, etc.

    thanks for the help
    jason

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Catch different types of exceptions. The exception names are generally pretty informative as to what causes them. Look up the different types of exceptions db work can throw then catch more specific exceptions. You can have multiple catch blocks after a try block.

    Your block will just catch any and all exceptions. You could do something like this
    Java Code:
    try{
      //do something risky
    } catch(IOException ioe){
      //do something to handle io exceptions
    } catch(FileNotFoundException fnfe){
      //do something else
    }
    Last edited by sunde887; 05-05-2011 at 08:25 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    That doesn't really work with JDBC since all exceptions are SQLExceptions, which cover a miriad of actual db exceptions.

    I'm sure the OP will correct me if I've misinterpreted what they're asking for, but I'm presuming it's how to identify the different SQLExceptions you can get (in their case a primary key violation).

    And the answer is...it's db specific. That's how Hibernate does it. Opens up the SQLException and looks for the error codes, which are different for different dbs.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Tolls View Post
    That doesn't really work with JDBC since all exceptions are SQLExceptions, which cover a miriad of actual db exceptions.

    I'm sure the OP will correct me if I've misinterpreted what they're asking for, but I'm presuming it's how to identify the different SQLExceptions you can get (in their case a primary key violation).

    And the answer is...it's db specific. That's how Hibernate does it. Opens up the SQLException and looks for the error codes, which are different for different dbs.
    But there are about 10 subclasses of the general SQLException class ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    Most of which are brand new (ie 1.6).
    Yes, you may be able to identify the exceptions now (assuming the driver you are using is 1.6 compliant, and many aren't, and the driver authors have updated such as to use these new classes) in a standard way...but I wouldn't bet on it.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Tolls View Post
    Most of which are brand new (ie 1.6).
    Yes, you may be able to identify the exceptions now (assuming the driver you are using is 1.6 compliant, and many aren't, and the driver authors have updated such as to use these new classes) in a standard way...but I wouldn't bet on it.
    That should teach me not to just skim over the API documentation; I think it'll be very difficult for the different DB vendors to implement those exceptions in a standard way, e.g. for one DB an error may be a RecoverableSQLException while for another it isn't ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    There are sub-sub ones that break them down further as well, so I think they've done a reasonable job on it, largely due to the work done on Hibernate I suspect, which fed JPA, which is where (I think) this lot comes from.

    I expect it will feed through, but old habits etc...

  8. #8
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default thanks

    I believe I was able to distinguish errors by doing this:
    Java Code:
    try { 
    //do work here
     } catch (SQLException e) {
                while (e != null) {
                    // Retrieve a human-readable message identifying the reason for the exception
                    String message = e.getMessage();
                    String sqlState = e.getSQLState();
                    int errorCode = e.getErrorCode();
    
                    String driverName = myConnection.getMetaData().getDriverName();
                    if (driverName.equals("JDBC-ODBC Bridge (ACEODBC.DLL)") && errorCode == 0 ) {
                        // Process error...
                        JOptionPane.showMessageDialog(this, "I'm sorry, you already have a district by this name, please choose a unique District Name.");
                         callingFrame.setColor(Color.red);           
                        callingFrame.setLable("Error:  You already have a district my this name.");
                    }
                    else{
                         callingFrame.setColor(Color.red);           
                        callingFrame.setLable("Error adding district:" + e.toString());
                    }
    
                    // The exception may have been chained; process the next chained exception
                    e = e.getNextException();
    
                   
                     
            }

Similar Threads

  1. Capturing the drawing area
    By George R. in forum Java 2D
    Replies: 7
    Last Post: 10-21-2010, 07:04 PM
  2. Capturing a screenshot of a another pc?
    By chyrl in forum Advanced Java
    Replies: 9
    Last Post: 06-08-2010, 05:07 PM
  3. capturing image
    By java_bond in forum Advanced Java
    Replies: 5
    Last Post: 03-04-2010, 07:45 AM
  4. help capturing input from drop down
    By javalearner1234 in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 05:00 AM
  5. MAC address capturing
    By viswa.tk in forum Java Applets
    Replies: 0
    Last Post: 12-13-2007, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •