我正在研究如何处理 Android 中的异常。
在update()
记事本内容提供程序的示例代码中的函数中,它调用getWriteableDatabase()
,这可能会引发SQLiteException
.
我注意到该NoteEditor
Activity
saveNote()
函数具有以下代码:
// Commit all of our changes to persistent storage. When the update completes
// the content provider will notify the cursor of the change, which will
// cause the UI to be updated.
try {
getContentResolver().update(mUri, values, null, null);
} catch (NullPointerException e) {
Log.e(TAG, e.getMessage());
}
如果发生了SQLiteException
会发生什么?我希望能够在 中捕获此异常Activity
并向用户显示适当的消息(通过吐司或类似的东西)。
我想我可以通过为SQLiteException
. 但是,我在 Google 文档中阅读了以下信息:
“请记住,Android 系统必须能够跨进程边界传递异常。Android 可以针对以下可能对处理查询错误有用的异常执行此操作:
IllegalArgumentException(如果您的提供者收到 >invalid content URI,您可以选择抛出此异常)
空指针异常"
所以我现在很困惑——我能不能抓住SQLiteException
?