ORMLite 不处理 SQLiteException?

发布于 2024-12-27 04:33:02 字数 2224 浏览 1 评论 0 原文

我特别小心地在我的应用程序中正确处理来自 ORMLite 的所有 SQLException,却发现在 ORMLite 下 android 的 sqlite 正在抛出运行时异常,就像他们根本不关心一样。

有什么方法可以避免处理 Android 的 SQLiteException 和 ORMLites SQLException?

下面是捕获 SQLException 的方法的示例堆栈跟踪,但显然不是 SQLiteException。

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-01-03 04:33:02

我不确定捕获多个异常的正确方法的正确答案是什么,但我将其视为 ORMLite 中的错误。在大多数地方(显然不是全部)ORMLite 尝试包装 Android 调用以捕获这些异常并将其重新抛出为 java.sql.SQLException。我已将检入主干的 AndroidConnectionSource 中的代码更改为以下内容:

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

我已 将此错误添加到跟踪器中,并检查了对主干的修复。将于 4.34 发布。如果您发现其他地方 ORMLite 没有正确包装这些异常,请告诉我。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文