【发布时间】:2013-06-03 21:31:13
【问题描述】:
我正在尝试调试以下代码:
public Cursor getUpdateTimestamps() {
Cursor timestamps;
try {
// int j = 3 / 0; // This will throw a divide-by-zero exception
timestamps = this.read_db.query(UPDATES_TABLE, new String[] { C_ID, U_C_TABLE, U_C_LAST_UPDATE }, null, null, null, null, null);
return timestamps;
} catch (Exception e) {
Log.e(TAG, "An error occurred in getUpdateTimestamps(): " + e.getMessage() + " | " + e.getCause());
return null;
}
}
我在将timestamps 设置为this.read_db.query... 的行上设置断点,并在我连接的设备(Samsung Galaxy S3)上启动我的调试器。
当我到达断点时,我会跳到下一条语句,这会导致抛出异常。但是,调试器没有转到我的catch 块中的第一行,而是直接跳到我的return null 语句而不记录我的错误消息。它还会跳过对System.out.println 的任何调用。
当我取消注释 int j = 3 / 0; 时,会引发并捕获正确的异常,并记录错误消息。
什么类型的异常可能导致Log.e() 方法被完全忽略?
更新:我还在我的日志中注意到以下堆栈跟踪,但不能确定它与这个问题有什么关系:
06-07 11:57:44.717 1757-1757/? W/System.err: android.database.sqlite.SQLiteConstraintException: column packagename is not unique (code 19)
06-07 11:57:44.717 1757-1757/? W/System.err: at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
06-07 11:57:44.717 1757-1757/? W/System.err: at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:857)
06-07 11:57:44.717 1757-1757/? W/System.err: at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
06-07 11:57:44.717 1757-1757/? W/System.err: at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
06-07 11:57:44.717 1757-1757/? W/System.err: at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665)
06-07 11:57:44.717 1757-1757/? W/System.err: at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
我的 SQLite 表都没有名为 packagename 的列,因此这可能无关。
【问题讨论】:
-
其他日志语句是否正常工作?
-
如问题所述,
When I uncomment int j = 3 / 0; the proper exception is thrown and caught, and the error message is logged. -
如果你试图捕捉 Throwable 会发生什么?
-
同样的事情 - 跳过对输出方法的任何调用(
t.printStackTrace()、Log.e(...)并直接转到return语句。 -
您发布的 logact 输出看起来与我对数据库查询异常的期望完全一样。您是否尝试过使用
Log.e(String, String, Throwable)而不是Log.e(String, String)?
标签: java android exception-handling try-catch