Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't SQLException catch SQLiteExcpetion?

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.

like image 550
Pzanno Avatar asked Nov 16 '11 22:11

Pzanno


People also ask

Does Exception catch SQLException?

SQLException is a specialized exception derived from Exception . If you catch Exception , all exception shall get caught. Even undesirable exceptions.

Is SQLException part of Exception?

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.

Can SQLException be thrown?

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.

Can we throw SQLException in Java?

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.


2 Answers

There are two types of SQLException:

android.database.SQLException
java.sql.SQLException

make sure you are using the first not the second.

like image 102
skynet Avatar answered Oct 02 '22 08:10

skynet


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.

like image 27
Vit Khudenko Avatar answered Oct 02 '22 07:10

Vit Khudenko