4

Hi in my project i need to handling the exception like duplicate value,empty field etc.. I thought of using the try catch method for this

public void deleteRegisterID(Object object) {
    SQLiteDatabase oDBLocal = this.getWritableDatabase();
    try {
        oDBLocal.execSQL("delete from " + STRTABLE_REGISTER + " where " + KEY_ID
                + " = " + object + ";");
    }catch (SQLException mSQLException) {
        Log.e("Loginerror", "getproductData >>" + mSQLException.toString());
        throw mSQLException;
    }
}

I am able to get the error but i need to handle it and sent some message to the user to enter again so i though of doing like this with help of this

catch (SQLException mSQLException)
    {
       switch (mSQLException.getErrorCode ())
       {
          case 19:
             //some toast message to user.
             break;
          case 11:
             //some toast message to user.
             break;
          default:
             throw mSQLException;
       }
     }

but there is no method as getErrorCode() in SQLException when i check mSQLException. but it is there in android document can any one help me

Jagan
  • 692
  • 1
  • 12
  • 38

1 Answers1

3

First i would like explain about SQLException class . In Android you can access 2 SQLException classes , android.database.SQLException and java.sql.SQLException . Your link have explanation about java.sql.SQLException class . Please check this link this will have information about android.database.SQLException class You might used android.database.SQLException class . we don't have getErrorCode() method in this class .

Temperately solution i can give for your problem . You can use following implementation which can solve your problem .

catch (SQLException mSQLException) {
            if(mSQLException instanceof SQLiteConstraintException){
                //some toast message to user.
            }else if(mSQLException instanceof SQLiteDatatypeMismatchException) {
                //some toast message to user.
            }else{
                throw mSQLException;
            }
        }

You can choose any subclasses from the list of subclasses in this link

Nagarajan
  • 238
  • 1
  • 7