Android SQLExceptions list

623 views Asked by At

I'm trying to make an article about catching SQL exceptions in Android. I want just make a list of all possible types of exceptions with description when they can occur.

I of course have visited sites like Android SQLException but there is very little information... just names of exceptions.

For an example (This example is quite obvious but I want them all in one article): android.database.sqlite.SQLiteException: no such column: test (code 1) - thrown when column in query is wrong. It could be useful to handle this in upgrading of database. The error message contains name of the missing column and sqlite error code which in this case means: SQL error or missing database

Just share your expierience with SQL exceptions in Android. Thanks in advance!

1

There are 1 answers

1
Michał Kołodziejski On

If you really want make a list of all possible types of exceptions, then asking a community to share their experience will not be sufficient to reach the goal. I'm afraid you'll have to put a lot of more effort to complete your task. Which may be worth it, of course!

I'd suggest you to do two things:

  1. inspect the Android's source code;
  2. inspect SQLite documentation, especially its result codes (https://www.sqlite.org/c3ref/c_abort.html) and maybe source code as well.

If it's about Android, its source is really well documented and the code is clean. At first, you may start with semi-automatic approach - rgrep through the java files. You can find there a lot of information you're looking for:

$ rgrep SQLite . | grep Exception |grep throw
./sqlite/SQLiteStatement.java:     * @throws android.database.sqlite.SQLiteDoneException if the query returns zero rows
[...]
./sqlite/SQLiteOpenHelper.java:                    throw new SQLiteException("Can't upgrade read-only database from version " +
./sqlite/SQLiteOpenHelper.java:        throw new SQLiteException("Can't downgrade database from version " +
./sqlite/SQLiteQueryBuilder.java:     * If the SQL statement is not valid, this method will throw a {@link SQLiteException}.
[...]

Then you can go one step further - browse it manually. You may find out that SQLite*Exceptions are not the only ones that are thrown. For example, in android.database.DatabaseUtils you may find such a method:

public static final void writeExceptionToParcel(Parcel reply, Exception e) {
    int code = 0;
    boolean logException = true;
    if (e instanceof FileNotFoundException) {
        code = 1;
        logException = false;
    } else if (e instanceof IllegalArgumentException) {
        code = 2;
    } else if (e instanceof UnsupportedOperationException) {
        code = 3;
    } else if (e instanceof SQLiteAbortException) {
        code = 4;
    } else if (e instanceof SQLiteConstraintException) {
        code = 5;
    } else if (e instanceof SQLiteDatabaseCorruptException) {
        code = 6;
    } else if (e instanceof SQLiteFullException) {
        code = 7;
    } else if (e instanceof SQLiteDiskIOException) {
        code = 8;
    } else if (e instanceof SQLiteException) {
        code = 9;
    } else if (e instanceof OperationApplicationException) {
        code = 10;
    } else if (e instanceof OperationCanceledException) {
        code = 11;
        logException = false;
    } else {
        reply.writeException(e);
        Log.e(TAG, "Writing exception to parcel", e);
        return;
    }
    reply.writeInt(code);
    reply.writeString(e.getMessage());

    if (logException) {
        Log.e(TAG, "Writing exception to parcel", e);
    }
}

You'd need to find the origin of codes and document it. Or maybe it's already documented somewhere - I don't know.

To conclude, you're trying to do something that may seem easy, but in fact it's not. If you want to describe all kinds of exceptions that may occur, it requires a lot of work. Decide if that's what you want, or maybe it'll be enough to write about the most common exceptions.

By the way, I'd argue if "It could be useful to handle this in upgrading of database" in case of SQLiteException: no such column: test (code 1) exception. This means that schema doesn't match the code. It should fail as fast as possible - it's the best way of getting to know your code is invalid and that it needs to be fixed. But that's just my remark not concerning the essence of your question.