Should a method in a data access object (DAO) throw or catch its exception?

I have a Java method in a data access object. This very simple method inserts two integer values ​​into the database.

public void saveHourMin(int hour, int min) throws SQLException{ psInsert.setInt(1, hour); psInsert.setInt(2, min); psInsert.executeUpdate(); } 

If this method, or, generally speaking, any DAO method, throws an exception when calling SQLException, or should it catch and register an exception, then inform the user through the return code? Which approach is suitable for the application using Spring?

+6
source share
3 answers

You cannot rely on the caller to constantly check return codes; you better exclude the exception.

If you throw a SQLException then it will be messy, you will probably end up with higher levels by adding a “throws Exception” for each method or just by throwing exceptions. None of these alternatives are good.

Spring's way is that it delivers an exception translator that takes the original SQLException and throws a subclass of RuntimeException, which includes the original SQLException as the reason, and which tries to provide as much error information as possible, including using the vendor's error codes, to decide which particular subclass to throw. If you use any of Spring jdbcTemplates, you get the exception translation functionality, so you don’t need to include any exceptions or throwing data access objects.

If you don't want to use Spring, you can catch a SQLException inside the DAO and throw a RuntimeException, including the original SQLException as the reason. You can’t do anything for most SQLExceptions, you just want to exit quickly and get an exception.

+4
source

I would say no. Catch a SQLException, and then throw a RuntimeException or one if the descendants. You do not want to pollute your application with data access exceptions. For example, you should not capture SQLExceptions in the GUI layer. I would also not create an application based on return codes.

Throwing a RuntimeException, you do not force callers to catch it as well.

+1
source

I would create a new exception called a DAOException, which is NOT a RuntimeException, and force the method user to handle such exceptions, possibly an enumeration as a member of the DAOException that describes the exception (possibly adding the actual exception internally as an internal exception).

So an exception might look like this:

 new DAOException(DAOException.TYPE.SQLE, e) 

and the saveHourMin method throws a DAOException.

Thus, if you have all kinds of problems, all this with the same exception, and you do not need to handle different ones.

I suggest using a general exception rather than a Runtime, because I don't want it to be optional to handle the exception. Anyone who calls this method should be aware that such a problem can occur and provide appropriate handling (even if it means that you are throwing a new RuntimeException, God forbid, but now it depends on them and this is their solution). As a rule, I avoid using RuntimeExceptions because they make the execution path unclear ("someone will probably catch it somewhere along the execution path, or he will kill the application, so letting go of it normally" doesn't sound good to me) .

+1
source

All Articles