Home » Android » java – Why doesn't SQLException catch SQLiteExcpetion?

java – Why doesn't SQLException catch SQLiteExcpetion?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have some code in one of my android apps that catches SQLException, but I’ve recently found that it’s not catching SQLiteException’s. Clearly, SQLiteException is a child of SQLException, so why isn’t it being caught? Here’s some code I’m using.

try {
    ... // something here that will throw an SQLiteException
} catch (SQLException e) {
    e.printStackTrace();
}

Is my assumption correct that I should be catching SQLiteException?

If it matters at all, i’m using this code not inside of an Activity, but inside of a class that extends the Application class.

As a side note, I did add an additional catch( Exception e ){} to see if that would work and it did indeed work as expected.

How to&Answers:

There are two types of SQLException:

android.database.SQLException
java.sql.SQLException

make sure you are using the first not the second.

Answer:

Is my assumption correct that I should be catching SQLiteException?

Are you talking about java.sql.SQLException or android.database.SQLException?

SQLiteException extends android.database.SQLException. Check your imports.