Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLiteException not being caught

I'm trying to catch an "android.database.sqlite.SQLiteException: error code 5: database is locked" exception with:

    try {
        db.insert("mytable", null, myvalues);
    } catch(SQLiteException e) {
        Log.d("My App", "caught");

        ...
    }

For some reason, I still get the error, and "caught" doesn't show up in LogCat. I tried catching a general "exception", but that still doesn't work. What's wrong?

UPDATE I found the issue, and it is really weird: for some reason changing db.insert() to db.insertOrThrow() as goto10 stated magically fixed everything. The error was comming from that line, but maybe it wasn't throwing an exception and only crashing or something?

like image 867
zim333311 Avatar asked Dec 27 '22 10:12

zim333311


1 Answers

I don't believe that .insert will throw an exception. You're probably just seeing a log message that's being written by it when it catches the exception internally. If you want it to throw an exception when an insert fails, use .insertOrThrow instead.

like image 139
goto10 Avatar answered Jan 03 '23 08:01

goto10