Can anyone think of a rational reason why SQLException
is a checked exception?
Yes, there could be a syntax error in the query
Yes, the connection might have died
Yes, there might be a permission problem
Etc, etc blah blah blah
But practically 100% of the time (once you're running in production), there isn't any problem.
If there is a problem, the calling code can't do anything to recover, so for that reason it should be unchecked.
Being checked create masses of perfunctory try catch
blocks throughout the code, as anyone who has been involved with a project that uses JDBC will attest. The code clutter is significant.
Because of the esoteric nature of SQL, the myriad of reasons you may get an SQLException and their complexity means you basically can not recover, unless the exception is caused by temporary network problem, but even then in a synchronous call, you're sunk anyway because you can't wait indefinitely for the network problem to be resolved, so you're going to have to fail the transaction.
Typically, calling SQL looks like this:
try {
// make some SQL call(s)
} catch {SQLException e) {
// log the exception
return; // and give up
}
Such code adds no value. There nothing reasonable you can do to recover. You might as well let a runtime exception bubble up - ie SQLException should be a runtime (unchecked) exception.
catch Exception
in all good apps that would catch an unchecked exception, just like it catches NPEs; just because exceptions are unchecked doesn't mean your "application will crash". And you're still welcome to catch specific unchecked exceptions if you want to. – ArrheniusSQLException
is practically anError
: You basically can't recover from them - your app is hosed - andErrors
are unchecked. Why notSQLException
too. – Arrhenius