簡體   English   中英

如果我在實現該接口的方法中拋出異常,我怎么能不在接口方法簽名處拋出異常?

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

我為每個實體都有 Dao 接口。我也為每個資源實現了這些接口,例如 MYSQL.MYSQL Dao 方法拋出特定的異常,所以我需要在接口層拋出它們,但異常是特定於 Z14498B83DD1667A0C78F4ZF5在接口層?或者我需要改變設計嗎? 例如,我有 UserDao 接口:

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;
}

我已經實現了所有這些,但我只展示了一個

MYSQLUserDao class insertUser方法的實現

@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;
    }

如果我想為 Oracle 實現 dao,我需要向 OracleDb 拋出特定異常,因此接口的方法簽名將有太多異常。 我該如何解決這個問題?

我至少可以想到兩種方法來實現這一目標:

  • 不要在實現中重新拋出MySQL*exceptions。 只需記錄它們。
  • 捕獲它們並將它們封裝為您自己的自定義異常

閱讀本文以了解處理異常的一些已知最佳實踐: https://dzone.com/articles/9-best-practices-to-handle-exceptions-in-java

我相信你的方法太老了。 幾乎所有的開發者都使用某種 ORM,例如 Hibernate、MyBatis 等...

所以,在我看來,你必須使用 ORM。 它節省了很多時間。 例如,如果您使用 Maven 項目,只需將 hibernate 依賴項添加到您的 pom.xml

或者

您必須編寫自定義異常 class 並使用自定義 class。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM