Question

I've been particularly careful to handle all the SQLExceptions from ORMLite properly in my app, only to discover that underneath ORMLite android's sqlite is throwing runtime exceptions like they just don't care.

Is there any way to avoid having to handle Android's SQLiteExceptions AND ORMLites SQLExceptions?

Here is a sample stack trace from a method which catches SQLExceptions, but obviously not SQLiteExceptions.

E/AndroidRuntime( 7308): Caused by: android.database.sqlite.SQLiteException: unable to open database file
E/AndroidRuntime( 7308):    at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
E/AndroidRuntime( 7308):    at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1821)
E/AndroidRuntime( 7308):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
E/AndroidRuntime( 7308):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
E/AndroidRuntime( 7308):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
E/AndroidRuntime( 7308):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
E/AndroidRuntime( 7308):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
E/AndroidRuntime( 7308):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
E/AndroidRuntime( 7308):    at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:60)
E/AndroidRuntime( 7308):    at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:50)
E/AndroidRuntime( 7308):    at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:189)
E/AndroidRuntime( 7308):    at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:153)
E/AndroidRuntime( 7308):    at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:245)
E/AndroidRuntime( 7308):    at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:250)
E/AndroidRuntime( 7308):    at au.com.ninthavenue.android.notes.application.TagsEM.getRecentTags(TagsEM.java:229)
E/AndroidRuntime( 7308):    at au.com.ninthavenue.android.notes.activities.EditNote.loadTags(EditNote.java:257)
E/AndroidRuntime( 7308):    at au.com.ninthavenue.android.notes.activities.EditNote.onCreate(EditNote.java:119)
Was it helpful?

Solution

I'm not sure what the proper answer is here in terms of proper ways to catch multiple exceptions, but I see it as a bug in ORMLite. In most places (obviously not all) ORMLite tries to wrap the Android calls to catch and re-throw these exceptions as java.sql.SQLExceptions. I've changed the code in AndroidConnectionSource checked into trunk to be the following:

SQLiteDatabase db;
try {
    db = helper.getWritableDatabase();
} catch (android.database.SQLException e) {
    throw SqlExceptionUtil.create("Unable to get writable database", e);
}
connection = new AndroidDatabaseConnection(db, true);

I've added this bug to the tracker and checked in the fix to trunk. It will be in 4.34. Let me know if you find other places were ORMLite is not properly wrapping these exceptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top