简体   繁体   中英

How can I not throw exception at interface method signature If I throw exception in method that implements that interface?

I have Dao interfaces for each entity.Also I implement those interfaces for each resources, example MYSQL.MYSQL Dao methods throw specific exception, so I need throw them at interface layer, but Exceptions are specific for MYSQL, So how can I not throw It at Interface layer?Or Do I need change design? Example, I have UserDao Interface:

public interface UserDao {
    boolean insertUser(Connection connection, User user) throws MySQLEXContainer.MySQLDBNotUniqueException, MySQLEXContainer.MySQLDBLargeDataException, MySQLEXContainer.MySQLDBExecutionException, SQLException;
    boolean deleteUser(Connection connection,String login) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    boolean validateUser(Connection connection,String login,String password) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    User findUser(Connection connection,String login) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    boolean updateUser(Connection connection,String login,String newPassword) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    List<User> findAllUser(Connection connection) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
}

I have Implemented all of them, but I show only one

MYSQLUserDao class implementation of insertUser method

@Override
    public boolean insertUser(Connection connection, User user) throws MySQLEXContainer.MySQLDBNotUniqueException, MySQLEXContainer.MySQLDBLargeDataException, MySQLEXContainer.MySQLDBExecutionException, SQLException {
        LOGGER.debug("Insert User Is started");
        int rowNum;
        ResultSet keys = null;
        Connection con;
        PreparedStatement statement = null;
        try {
            String query = QueriesUtil.getQuery("insertUser");
            con = connection;
            statement = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            statement.setString(1, user.getLogin());
            statement.setString(2, PasswordUtil.generateStrongPasswordHash(user.getPassword()));
            statement.setString(3, user.getUserType());
            statement.setString(4, user.getUserEmail());
            rowNum = statement.executeUpdate();
            keys = statement.getGeneratedKeys();
            if (keys.next()) {
                user.setUserId(keys.getInt(1));
            }
        } catch (SQLException e) {
            LOGGER.error(e);
            if (e.getErrorCode() == 1062) {
                throw new MySQLEXContainer.MySQLDBNotUniqueException(String.format("%s %s", user.getLogin(), user.getUserEmail()), e.getCause());
            } else if (e.getErrorCode() == 1406) {
                throw new MySQLEXContainer.MySQLDBLargeDataException("Data Is too long", e);
            }
            throw new MySQLEXContainer.MySQLDBExecutionException("Bad execution", e);
        } finally {
            ConnectionUtil.oneMethodToCloseThemAll(keys, statement, null);
            LOGGER.debug("Close all resources");
        }
        return rowNum > 0;
    }

If I want to implement dao for Oracle I will need to throw specific exception to OracleDb and therefore Interface's methods signature will have too many exceptions. How can I solve that problem?

I can think at least two ways to achieve that:

  • Do not rethrow the MySQL*exceptions in the implementation. Just log them.
  • Catch them and encapsulate them as a custom Exception of your own

Read this to learn some known best practices at handling exceptions: https://dzone.com/articles/9-best-practices-to-handle-exceptions-in-java

I believe your method is so old. Almost all developers use some kind of ORM, such as Hibernate, MyBatis etc...

So, in my opinion, you have to use ORM. It saves a lot of time. For example, if you are using Maven project, just add hibernate dependency to your pom.xml

OR

You have to write custom Exception class and use your custom class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM