Why does SQLException not catch SQLiteExcpetion?

I have code in one of my Android apps that catches a SQLException, but I recently discovered that it did not catch a SQLiteException. Obviously, SQLiteException is a child of SQLException, so why won't it get caught? Here is the code I'm using.

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

I suggest that I should catch a SQLiteException?

If that matters at all, I use this code not inside the Activity, but inside a class that extends the Application class.

As an extra note, I added an extra catch( Exception e ){} to see if this will work, and it really works as expected.

+8
java android exception
source share
2 answers

There are two types of SQLException:

 android.database.SQLException java.sql.SQLException 

make sure you use the first, not the second.

+16
source share

I suggest that I should catch a SQLiteException?

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

SQLiteException extends android.database.SQLException . Check import.

+3
source share

All Articles