2014-02-07 13 views
4

嗨處理。我想到了使用嘗試捕捉方法,這的SQLException錯誤在我的項目,我需要像處理重複值,空場等異常Android中

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

的我能得到錯誤,但我需要處理,並重新發送一些信息給用戶輸入,所以我雖然做這樣與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; 
     } 
    } 

幫助,但沒有方法getErrorCode()中的SQLException當我檢查mSQLException.但在那裏Android的文檔可以幫助我的任何人

回答

3

首先我想解釋一下關於SQLException類。在Android中,您可以訪問2個SQLException類,android.database.SQLExceptionjava.sql.SQLException。你的鏈接有關於java.sql.SQLException類的解釋。請檢查this鏈接,這將有關於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; 
      } 
     } 

您可以選擇從子類的列表中的任意子類在此link