SQLException error handling in android

Hi, in my project I need to handle an exception such as a repeating value, an empty field, etc. I was thinking about 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 can get an error, but I need to process it and send a message to the user to log in again, so although I do it using 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 like getErrorCode()in SQLException when I check mSQLException., but it is in the android document. Can anybody help me.

+4
source share
1 answer

SQLException. Android 2 SQLException, android.database.SQLException java.sql.SQLException. java.sql.SQLException. , , android.database.SQLException. android.database.SQLException. getErrorCode() .

, . , .

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;
            }
        }

+3

Source: https://habr.com/ru/post/1525736/


All Articles