java - Why is SQLException a checked exception -


can think of rational reason why sqlexception checked exception?

yes, there syntax error in query
yes, connection might have died
yes, there might permission problem
etc, etc blah blah blah

but practically 100% of time (once you're running in production), there isn't problem.

if there problem, calling code can't recover, reason should unchecked.

being checked create masses of perfunctory try catch blocks throughout code, has been involved project uses jdbc attest. code clutter significant.

because of esoteric nature of sql, myriad of reasons may sqlexception , complexity means can not recover, unless exception caused temporary network problem, in synchronous call, you're sunk anyway because can't wait indefinitely network problem resolved, you're going have fail transaction.

typically, calling sql looks this:

try {     // make sql call(s) } catch {sqlexception e) {      // log exception     return; // , give } 

such code adds no value. there nothing reasonable can recover. might let runtime exception bubble - ie sqlexception should runtime (unchecked) exception.

practically 100% of time there isn't problem - limited own observation says nothing other systems. there various computer systems around world various bottlenecks. success rate 100%. others have deal lower percentage.

common misconception consider introducing/removing checked exception frequency of occurrence. checked exceptions serve communication channels. know, every method has public interface. way, method tells arguments accepts , result of code in body.

when becomes impossible method being in progress keep promise (e.g. returned value) needs way tell other method went wrong , can't expected. how ? sending message value returned doesn't work, there no chance calling method distinguish between proper value , error message. not methods have void return value. do when you're unable keep promise defined method's interface ? well, throw exception (send message).

if expect resultset , there no connection database established, should ? return empty resultset ? hell no, tells database empty. return null ? well, delegates problem , makes finding cause unclear.

you use empty resultset , make part of query database, making inconsistent.

without sqlexception, 1 mistake lead data inconsistency.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -