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.
SQLException is a specialized exception derived from Exception . If you catch Exception , all exception shall get caught. Even undesirable exceptions.
Class SQLException. An exception that provides information on a database access error or other errors. Each SQLException provides several kinds of information: a string describing the error.
The following subclasses of SQLException can also be thrown: BatchUpdateException is thrown when an error occurs during a batch update operation. In addition to the information provided by SQLException , BatchUpdateException provides the update counts for all statements that were executed before the error occurred.
SQLException is available in the java. sql package. It extends the Exception class which means that we can use the methods available in the Exception class in the SQLException class as well.
There are two types of SQLException:
android.database.SQLException
java.sql.SQLException
make sure you are using the first not the second.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With