Why does my exception handler not trap Android SQLite insert error?

While not 100% related to the question, I ran into a similar problem using Room and Google returned this question for my search. In the case of Room, there doesn’t appear to be a catchable exception thrown and insertOrThrow() does not exist. In reviewing https://developer.android.com/reference/kotlin/androidx/room/OnConflictStrategy#ABORT:kotlin.Int, many of the options are depreciated, but I went with OnConflictStrategy.IGNORE here since this will return -1 if there is a problem.

Cheers.


I found the answer here: SQLiteConstraintException not caught

The SQLiteDatabase.insert() method doesn't throw an exception. d'oh!

For other SQLite newbies like me, if you want to catch exceptions when inserting into the database, use the SQLite.insertOrThrow() method. It will throw an exception which you can then catch and handle.