Why is it advisable to handle java.sql.SQLException
within DAO (Data Access Object) class file, and
throwing application specific exception to the
application or client code?
Third party Advertisement
Ans:
As Data Access Object design pattern is used to de-
couple client or caller code from the data access
code, so that change in the data access implementation
don't impact change in client or caller code. Then by
throwing SQLException to client fails this basic de-
coupling idea. So it is always advisable to handle
SQLException within DAO and throw application specific
exception back to the user with proper application
related error message. Reason is to reduce coupling
of client/application code from the DAO code, not only
from the data, but also from the exceptions as well. By
doing this there is no need for the client code to handle
SQLException (that is thrown from the DAO code) and by
doing this, client/application using DAO can still be
immune to the type of Persistence arrangement used in the
application. Suppose persistence/data access for the
application changes from Database (RDBMS) to web services,
XML based data store, or Object oriented database, then
also client/application caller code will be saved from
replacing SQLException to some other exception specific
to the new data store.
Third party Advertisement
Add your suggestions here