我一直特别小心在我的应用程序中正确处理来自ORMLite的所有SQLExceptions,但却发现在ORMLite下面,android的sqlite会抛出运行时异常,就像他们根本不关心。
有没有办法避免必须处理Android的SQLiteExceptions和ORMLites SQLExceptions?
以下是来自捕获SQLExceptions的方法的示例堆栈跟踪,但显然不是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)
答案 0 :(得分:1)
我不确定在抓住多个异常的正确方法方面这里有什么正确的答案,但我认为它是ORMLite中的一个错误。在大多数地方(显然不是全部),ORMLite尝试包装Android调用以捕获并重新抛出这些异常java.sql.SQLException
。我已将AndroidConnectionSource中检入到trunk的代码更改为以下内容:
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);
我added this bug跟踪器并检查了修复程序。它将在4.34。如果您发现ORMLite没有正确包装这些例外,请告诉我。