Search code examples
javaandroidsqlexception

Why is android.database.SQLException unchecked?


This might be unsuitable for SO since it isn't really a coding-problem, but I couldn't find a satisfying answer yet and I believe the SO-community can.

So, by definition android.database.SQLException and java.sql.SQLException share about the same scope of application, which is to provide information about errors while accessing/modifying a database. Though I've read somewhere that checked exceptions are "out", I really like the fact that the compiler reminds you of handling exceptions when you use checked exceptions with the throws keyword. Unfortunately this doesn't work with unchecked exceptions.

My question is: Why did Google make their android.database.SQLException unchecked when java.sql.SQLException is checked? Am I missing something? Do they differ more than I think?


Solution

  • The choice between making an exception checked or unchecked is always somewhat subjective:

    • If an exception indicates a bug, or some failure that is "probably not recoverable", then a unchecked exception is appropriate.

    • If an exception indicates a failure that is "probably recoverable", then a checked exception is appropriate.

    It is more difficult in cases like this where there is a general exception with lots of subclasses. Some of those subclasses could be bugs, or they could be recoverable. At this point, the designer has to make a value judgement about the relative likelihood of the different cases ... across all known / predicted subtypes of the exception.

    In this case, the Sun and Google engineers came to different conclusions. But note that the Google engineers had a big advantage that the Sun engineers didn't have. They could look at the Java design, and make their own judgement on how well it worked with a checked SQLException.


    Though I've read somewhere that checked exceptions are "out" ...

    There are a lot of developers who wish that all Java exceptions were unchecked so that they didn't have to be forced to deal with them. There are a lot of other developers who still think that Java got it right with checked exceptions ... even though there were a few cases where the wrong choice was made (in hindsight).

    It is debatable.

    Am I missing something? Do they differ more than I think?

    Not really. The two exception hierarchies serve pretty much the same purpose ... differences in javadoc class descriptions notwithstanding.