Vijay
Vijay

Reputation: 1388

try/catch not working and crashes app android

I am inserting some data in my SQLite database android. I created an ArrayList of Content values. Below is my code.

try {
                SQLiteDatabase db = this.getWritableDatabase();
                db.beginTransaction();
                for (int i = 0; i < arrayListAudio.size(); i++) {
                    db.insert(TABLE_AUDIO_FILES, null, arrayListAudio.get(i));
                }
                for (int i = 0; i < arrayListText.size(); i++) {
                    db.insert(TABLE_TEXT_FILES, null, arrayListText.get(i));
                }
                db.setTransactionSuccessful();
                db.endTransaction();
                db.close();
                //arrayListText.clear();
                //arrayListAudio.clear();
} catch (SQLException e) {
       Log.d("vijaysee", e.getMessage());
}

It works but sometimes it does not work and crashes the application

My problem is that it crashes the application directly because try/catch not working. If there is any problem in the code then it should call catch but instead, it crashes the application. I think that there should be some change in catch brackets like catch(// some change){} . Please help me with this.

Upvotes: 0

Views: 260

Answers (2)

Vijay
Vijay

Reputation: 1388

The reason why the app was crashing,

  1. I was not using synchronisation (in threads)
  2. I should catch using Exception class instead of specifying the exception.

Upvotes: 0

Martin Zeitler
Martin Zeitler

Reputation: 76839

You are catching a Java SQLException instead of an Android SQLiteException. And if you don't know what to catch, always try to catch an Exception. I'd rather worry about the SQL statement: INSERT INTO textFiles(null) VALUES (NULL) (which neither features columns nor values).

Upvotes: 1

Related Questions